This article describes the application of the php verification code function with examples. For more information, see
The code is as follows:
Function _ code ($ _ code_length = 4, $ _ width = 75, $ _ height = 25 ){
For ($ I = 0; $ I <$ _ code_length; $ I ++ ){
$ _ Nmsg. = dechex (mt_rand (0, 15 ));
}
$ _ SESSION ["code"] =_ _ nmsg;
$ _ Img = imagecreatetruecolor ($ _ width, $ _ height );
$ _ White = imagecolorallocate ($ _ img, 250,250,250 );
Imagefill ($ _ img, 0, 0, $ _ white );
$ _ Gray = imagecolorallocate ($ _ img, 196,196,196 );
Imagerectangle ($ _ img, 0, 0, $ _ width-1, $ _ height-1, $ _ gray );
For ($ I = 0; $ I <6; $ I ++ ){
$ _ Md_color = imagecolorallocate ($ _ img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
Imageline ($ _ img, mt_rand (0, $ _ width), mt_rand (0, $ _ height), mt_rand (0, $ _ width), mt_rand (0, $ _ height), $ _ md_color );
}
For ($ I = 0; $ I <50; $ I ++ ){
$ _ Md_color = imagecolorallocate ($ _ img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
Imagestring ($ _ img, 1, mt_rand (1, $ _ width-5), mt_rand (1, $ _ height-5), "*", $ _ md_color );
}
For ($ I = 0; $ I <$ _ code_length; $ I ++ ){
$ _ Md_color = imagecolorallocate ($ _ img, mt_rand (0,102), mt_rand (0,102), mt_rand (0,102 ));
Imagestring ($ _ img, 5, $ I * $ _ width/$ _ code_length + mt_rand (1, 10), mt_rand (1, $ _ height/2 ), $ _ SESSION ["code"] [$ I], $ _ md_color );
}
Header ("Content-Type: image/png ");
Imagepng ($ _ img );
Imagedestroy ($ _ img );
}