PHP verification code function code (simple and practical ). : Copy the code as follows :? Php *** vCode (m, n, x, y) m numbers are displayed in the n-side width x-side height y * micxp * jb51.net * session_start (); vCode (4, 15); 4 digits, display:
The code is as follows:
/**
* The value of vCode (m, n, x, y) m is n-side width x-side height y.
* Micxp
* Jb51.net
*/
Session_start ();
VCode (4, 15); // 4 digits. the display size is 15.
Function vCode ($ num = 4, $ size = 20, $ width = 0, $ height = 0 ){
! $ Width & amp; $ width = $ num * $ size * 4/5 + 5;
! $ Height & $ height = $ size + 10;
// Remove 0 1 O l and so on
$ Str= "23456789 abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW ";
$ Code = '';
For ($ I = 0; $ I <$ num; $ I ++ ){
$ Code. = $ str [mt_rand (0, strlen ($ str)-1)];
}
// Draw an image
$ Im = imagecreatetruecolor ($ width, $ height );
// Define the color to be used
$ Back_color = imagecolorallocate ($ im, 235,236,237 );
$ Boer_color = imagecolorallocate ($ im, 118,151,199 );
$ Text_color = imagecolorallocate ($ im, mt_rand (0,200), mt_rand (0,120), mt_rand (0,120 ));
// Draw the background
Imagefilledrectangle ($ im, 0, 0, $ width, $ height, $ back_color );
// Draw a border
Imagerectangle ($ im, 0, 0, $ width-1, $ height-1, $ boer_color );
// Draw interference lines
For ($ I = 0; $ I <5; $ I ++ ){
$ Font_color = imagecolorallocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagearc ($ im, mt_rand (-$ width, $ width), mt_rand (-$ height, $ height), mt_rand (30, $ width * 2), mt_rand (20, $ height * 2), mt_rand (0,360), mt_rand (0,360), $ font_color );
}
// Draw interference points
For ($ I = 0; $ I <50; $ I ++ ){
$ Font_color = imagecolorallocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagesetpixel ($ im, mt_rand (0, $ width), mt_rand (0, $ height), $ font_color );
}
// Draw the verification code
@ Imagefttext ($ im, $ size, 0, 5, $ size + 3, $ text_color, 'C: \ WINDOWS \ Fonts \ simsun. ttc ', $ code );
$ _ SESSION ["VerifyCode"] = $ code;
Header ("Cache-Control: max-age = 1, s-maxage = 1, no-cache, must-revalidate ");
Header ("Content-type: image/png; charset = gb2312 ");
Imagepng ($ im );
Imagedestroy ($ im );
}
?>
The pipeline code is as follows :? Php/*** vCode (m, n, x, y) m numbers are displayed in the n-side width x-side height y * micxp * jb51.net */session_start (); vCode (4, 15); // 4 digits, displaying...