Home > Backend Development > PHP Tutorial > Generate verification code in php_PHP tutorial

Generate verification code in php_PHP tutorial

WBOY
Release: 2016-07-13 10:45:23
Original
793 people have browsed it

The principle of this PHP verification code generation code is to generate random numbers-->Create pictures-->Write random numbers into pictures-->Save them in the session. Take a look at the process of verification code picture generation to notify the browser that it will output a PNG picture. Good random number generator seed srand((double)microtime()*1000000); draw the four-digit integer verification code into the picture

/php tutorial to generate verification code
/*================================================ =====
The principle of this PHP code to generate verification code is to generate random numbers--> Create a picture--> Write the random number into the picture--> Save it in the session. Take a look at the process of verification code picture generation. Notify the browser that a png picture is about to be output. Good random number generator seed srand((double)microtime()*1000000); Draw the four-digit integer verification code into the picture
================================================== =====*/

/*=====================
Generate random string function
=====================*/
function random($length) {
$hash = '';
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}

//Verification code image generation

session_start();
//Notify the browser that png images will be output
header("content-type: image/png");
//Get ready the random number generator seed
//srand((double)microtime()*1000000);
//Prepare the relevant parameters of the image
$im = imagecreate(62,22);
$black = imagecolorallocate($im, 0,0,0); //rgb black identifier
$white = imagecolorallocate($im, 255,255,255); //rgb white identifier
$gray = imagecolorallocate($im, 179,183,185); //rgb gray identifier
//Start drawing
Imagefill($im,0,0,$gray);
//while(($randval=rand()%100000)<10000);{
​​​​ //$_session["check_code"] = $randval;
​​​​ //Draw the four-digit integer verification code into the picture
$randval=random(4);
$_session["check_code"]=$randval;
Imagestring($im, 5, 10, 3, $randval, $white);
//}
//Add interference pixels
for($i=0;$i<150;$i++){
          $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70, rand()%30, $white);
}
//Output verification image
Imagepng($im);
//Destroy image identifier
Imagedestroy($im);

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633022.htmlTechArticleThe principle of this PHP verification code generation code generates random numbers --> creates pictures --> writes random numbers into pictures -->Save in session, look at the process verification code image generation notification browser will output P...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template