Note: The following code needs to open the GD library of PHP, modify the configuration of the php. In file, and cancel the semicolon before the commented-out line: Extension = php_gd2.dll.
<?
Session_start ();
// Generate a verification code Image
Header ("Content-Type: image/PNG ");
$ Im = imagecreate (44,18 );
$ Back = imagecolorallocate ($ im, 245,245,245 );
Imagefill ($ im, 0, 0, $ back); // background
Srand (double) microtime () * 1000000 );
// Generate four digits
For ($ I = 0; $ I <4; $ I ++ ){
$ Font = imagecolorallocate ($ im, Rand (100,255), Rand (0,100), Rand (100,255 ));
$ Authnum = rand (1, 9 );
$ Vcodes. = $ authnum;
Imagestring ($ im, 5, 2 + $ I * 10, 1, $ authnum, $ font );
}
For ($ I = 0; $ I <100; $ I ++) // Add interference pixels
{
$ Randcolor = imagecolorallocate ($ im, Rand (0,255), Rand (0,255), Rand (0,255 ));
Imagesetpixel ($ im, Rand () % 70, Rand () % 30, $ randcolor );
}
Imagepng ($ IM );
Imagedestroy ($ IM );
$ _ Session ['vcode'] = $ vcodes;
?>
To the specific place of the call, the form is as follows: . You can verify the value of session: $ _ session ['vcode'] During verification. You can also slightly improve the above Code and change it to the form of adding and summation of two numbers. For more information, see ASP generate BMP image verification code: Add and sum of numbers.