This php code generation Verification Code principle generates a random number --> creates an image --> writes a random number into an image --> saves it in the session, take a look at the process verification code Image Generation notice the browser will output the PNG Image prepare the random number generator seed srand (double) microtime () * 1000000); the four-digit integer verification code is drawn into the image
/Php tutorial to generate Verification Code
/* ===================================================== ====================
This php code generation Verification Code principle generates a random number --> creates an image --> writes a random number into an image --> saves it in the session, take a look at the process verification code Image Generation notice the browser will output the png Image prepare the random number generator seed srand (double) microtime () * 1000000); the four-digit integer verification code is drawn into the image
========================================================== ===================== */
/* ================================
Generate random string functions
===================================== */
Function random ($ length ){
$ Hash = '';
$ Chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxy ';
$ 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 a png image will be output
Header ("content-type: image/png ");
// Prepare the random number generator Seeds
// Srand (double) microtime () * 1000000 );
// Prepare Image Parameters
$ Im = imagecreate (62,22 );
$ Black = imagecolorallocate ($ im, 0); // rgb black identifier
$ White = imagecolorallocate ($ im, 255,255,255); // rgb white identifier
$ Gray = imagecolorallocate ($ im, 179,183,185); // rgb gray identifier
// Start plotting
Imagefill ($ im, 0, 0, $ gray );
// While ($ randval = rand () % 100000) <10000 );{
// $ _ Session ["check_code"] = $ randval;
// Print the four-digit integer verification code into the image
$ Randval = random (4 );
$ _ Session ["check_code"] = $ randval;
Imagestring ($ im, 5, 10, 3, $ randval, $ white );
//}
// Add interference pixels
For ($ I = 0; I 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 the image identifier
Imagedestroy ($ im );
?>