How to create a PHP verification code? -Unique196 /**
* Create a verification code
* 1. start the session
* 2. set the header
* 3. create a canvas
* 4. create a color
* 5. create a random number and place it on the canvas.
* 6. add several random numbers to the session.
* 7. add interference points or interference lines
* 8. output canvas
* 9. destroy canvas resources
*/
// 1. start the session
Session_start ();
// 2. set the header to specify the MIME output type
Header ('content-Type: image/png ');
// 3. create a canvas
$ Width = 100;
$ Height = 30;
$ Im = imagecreate ($ width, $ height );
// 4. create a color
$ Bgcolor = imagecolorallocate ($ im, 255,255,255 );
$ Textcolor = imagecolorallocate ($ im, 0,255,255 );
$ Randcolor = imagecolorallocate ($ im, mt_rand (0,200), mt_rand (0,200), mt_rand (0,200 ));
// 5. create a random number and place it on the canvas.
$ Verify = null;
For ($ I = 0; $ I <4; $ I ++ ){
$ Temp = mt_rand (0, 9 );
$ Verify. = $ temp;
Imagestring ($ im, 5, $ I * 15 + 15, 8, $ temp, imagecolorallocate ($ im, mt_rand (0,200), mt_rand (0,200), mt_rand (0,200 )));
}
// 6 put the generated random number into the session
$ _ SESSION ['verify '] = $ verify;
// 7. add interference points
For ($ I = 0; I I <100; $ I ++ ){
Imagesetpixel ($ im, rand (0, $ width), rand (0, $ height), imagecolorallocate ($ im, rand (100,255), rand (100,255 ), rand (100,255 )));
}
// 8. output the image
Imagepng ($ im); // imagegif ()
// 9. destroy an image
Imagedestroy ($ im );
?>