Php verification code generation detailed tutorial. This article provides a php Tutorial to generate a verification code. the previous section describes how to generate and call the verification core code .? Phpheader (content-type: image this article provides a php Tutorial to generate a verification code. the previous section describes the core code for generating verification, followed by a method for generating and calling.
// Header ("content-type: image/png ");
$ Num = '000000 ';
$ Imagewidth = 60;
$ Imageheight = 18;
$ Numimage = imagecreate ($ imagewidth, $ imageheight );
Imagecolorallocate ($ numimage, 240,240,240 );
For ($ I = 0; $ I $ X = mt_rand (1, 8) + $ imagewidth * $ I/4;
$ Y = mt_rand (1, $ imageheight/4 );
$ Color = imagecolorallocate ($ numimage, mt_rand (0,150), mt_rand (0,150), mt_rand (0,150 ));
Imagestring ($ numimage, 5, $ x, $ y, $ num [$ I], $ color );
}
For ($ I = 0; I I <200; $ I ++ ){
$ Randcolor = imagecolorallocate ($ numimage, rand (200,255), rand (200,255), rand (200,255 ));
Imagesetpixel ($ numimage, rand () % 70, rand () % 20, $ randcolor );
}
Imagepng ($ numimage );
Imagedestroy ($ numimage );
?>
Let's look at an instance that generates a verification code.
// Generate a verification code Image
Session_start ();
Header ("content-type: image/png ");
Srand (double) microtime () * 1000000 );
$ Roundnum = rand );
// Store the random number in the session for future use
$ _ Session ["sessionround"] = $ roundnum;
$ Im = imagecreate (58,28 );
$ Red = imagecolorallocate ($ im, 255, 0, 0 );
$ Blue = imagecolorallocate ($ im, 0,255, 0 );
// Fill in the local area, which is equivalent to the background
Imagefill ($ im, 68, 30, $ red );
// Print the four-digit integer verification code into the image
Imagestring ($ im, 5, 10, 8, $ roundnum, $ blue );
For ($ I = 0; $ I <50; $ I ++) // add interference pixels
{
Imagesetpixel ($ im, rand () % 70, rand () % 30, $ black );
}
Imagepng ($ im );
Imagedestroy ($ im );
?>
Html call method
Verification code
Bytes. ? Php // header (content-type: image...