Home Backend Development PHP Tutorial 解决php session验证码不现实的有关问题

解决php session验证码不现实的有关问题

Jun 13, 2016 pm 12:16 PM
function quot rand

解决php session验证码不现实的问题

本人一开始图片老显示不出来,显示的是一个裂了的图片,经查询发现可能由以下几种可能造成

1)在header("Content-type:image/png");前加如下一段代码

ini_set('display_errors', 'Off');
Copy after login
本人就是由上面的问题造成。

附上生成验证码的图片的代码

<?php ini_set(&#39;display_errors&#39;, &#39;Off&#39;);	header("Content-type:image/png");	session_start();		$_SESSION[&#39;code&#39;] = &#39;null&#39;;			//初始化	$width = &#39;58&#39;;	$height = &#39;22&#39;;	//$code = &#39;&#39;;	$tmptext =&#39;&#39;;	$bordercolor= &#39;&#39;;	for($i=0;$i<4;$i++)	{		$tmptext = rand(0,9);		$code .= $tmptext;	}	$_SESSION[&#39;code&#39;] = $code;	//以下三句诗让浏览器不缓存	@header("Expires:-1");	@header("Cache-Control:no-store,private,posc-check=0,pre-check=0,max-age=0",FALSE);	@header("Pragma:no-cache");		if(function_exists(&#39;imagecreate&#39;) && function_exists(&#39;imagecolorset&#39;) && function_exists(&#39;imagecopyresized&#39;)			&& function_exists(&#39;imagecolorallocate&#39;) && function_exists(&#39;imagesetpixel&#39;) 			&& function_exists(&#39;imagechar&#39;)  &&  function_exists(&#39;imagecreatefromgif&#39;)			&& function_exists(&#39;imagepng&#39;)		)	{		$im = imagecreate($width, $height);		$backgroundcolor = imagecolorallocate($im, 255, 255, 255);		$numorder = array(1,2,3,4);		/**shuffle将数组打乱*/		shuffle($numorder);		/**array_flip返回一个被反转的数组,键值被处理值*/		$numorder = array_flip($numorder);		$x=&#39;&#39;;		$y=&#39;&#39;;		$text_color = &#39;&#39;;		for($i=1;$i<=4;$i++)		{			$x = $numorder[$i] * 13 + mt_rand(0,4) -2;			$y = mt_rand(0,3);			$text_color = imagecolorallocate($im, mt_rand(50,255), mt_rand(50,255), mt_rand(50,255));			/**将字符画在$im图中,5表示字体的大小 $x+5 */			imagechar($im, 5, $x+5, $y+3, $code[$numorder[$i]], $text_color);		}				$linenums = mt_rand(10,32);		for($i=0;$i<=$linenums;$i++)		{			$linecolor = imagecolorallocate($im, 255, mt_rand(0,255), mt_rand(0,255));			$linex = mt_rand(0,255);			$liney = mt_rand(0,255);			imageline($im, $linex, $liney, $linex+mt_rand(0,4)-2, $liney+mt_rand(0,4)-2,  $linecolor);		}				for($i=0;$i<40;$i++)		{			$pointcolor = imagecolorallocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));			imagesetpixel($im, mt_rand(0,$width), mt_rand(0,$height), $pointcolor);		}				$bordercolor = imagecolorallocate($im, 150, 150, 150);		imagerectangle($im, 0, 0, $width-1, $height-1, $bordercolor);				imagepng($im);		imagedestroy($im);	}?>
Copy after login



2)变量没初始化


3)在header("Content-type:image/png");前加入

ob_clean();



第二三中方法不确保可以解决问题,仅供参考。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve 'undefined: rand.Seed' error in golang? How to solve 'undefined: rand.Seed' error in golang? Jun 25, 2023 am 08:34 AM

During the development or learning process of using Golang, we may encounter the error message of undefined:rand.Seed. This error usually occurs when you need to use a random number generator, because in Golang you need to set a random number seed before you can use the function in the rand package. This article will explain how to resolve this error. 1. Introduce the math/rand package. First, we need to introduce the math/rand package into the code. exist

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

How to synchronize random number generation in Golang parallel processing? How to synchronize random number generation in Golang parallel processing? Jun 03, 2024 pm 02:53 PM

Synchronizing random number generation in Go concurrent programming: Use a mutex (sync.Mutex) to control access to the rand.Rand random number generator. Each goroutine acquires the mutex lock before generating random numbers and releases the mutex lock after generating it. This ensures that only one goroutine can access the random number generator at a time, eliminating data races.

How to solve the problem of generating the same random numbers using php rand function How to solve the problem of generating the same random numbers using php rand function Mar 23, 2023 am 09:17 AM

The rand() function uses the same initial seeds on each call. The default initial seed is obtained from the operating system's time, but it only has microsecond accuracy. That is, within a very short period of time, many rand() function calls will use the same initial seed, resulting in the same random number generation. So, how to solve this problem?

file_exists() function in PHP file_exists() function in PHP Sep 14, 2023 am 08:29 AM

The file_exists method checks whether a file or directory exists. It accepts as argument the path of the file or directory to be checked. Here's what it's used for - it's useful when you need to know if a file exists before processing it. This way, when creating a new file, you can use this function to know if the file already exists. Syntax file_exists($file_path) Parameters file_path - Set the path of the file or directory to be checked for existence. Required. Return file_exists() method returns. Returns TrueFalse if the file or directory exists, if the file or directory does not exist Example let us see a check for "candidate.txt" file and even if the file

See all articles