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);?>