This article mainly introduces the relevant information about the function for generating verification codes in php. For more information, see the function for generating verification codes in php. it is practical and reliable. The verification code generated first and foremost (the example of the verification code with full numbers is generated here ):
The following is the source code of the php verification code:
<? Php session_start (); // session_register ('checkcode'); // for PHP4.2 or later versions, you do not need to use session_register () to register the SESSION variable $ type = 'GIF'; $ width = 45; $ height = 20; header ("Content-type: image /". $ type); srand (double) microtime () * 1000000); if (isset ($ _ GET ['action']) {$ randval = randStr (4, $ _ GET ['action']);} else {$ randval = randStr (4, '');} if ($ type! = 'GIF' & function_exists ('imagecreatetruecolor') {$ im = @ imagecreatetruecolor ($ width, $ height);} else {$ im = @ imagecreate ($ width, $ height) ;}$ r = Array (225,211,255,223); $ g = Array (225,236,237,215); $ B = Array (225,236,166,125); $ key = rand ); $ backColor = ImageColorAllocate ($ im, $ r [$ key], $ g [$ key], $ B [$ key]); // background color (random) $ borderColor = ImageColorAllocate ($ im, 127,157,185); // border color $ pointColor = ImageColorAllocate ($ im, 255,170,255); // The dot color @ imagefilledrectangle ($ im, $ width-1, $ height-1, $ backColor); // background position @ imagerectangle ($ im, 0, 0, $ width-1, $ height-1, $ borderColor ); // border position $ stringColor = ImageColorAllocate ($ im, 255, 51, 153); for ($ I = 0; $ I <= 100; $ I ++) {$ pointX = rand (2, $ width-2); $ pointY = rand (2, $ height-2); @ imagesetpixel ($ im, $ pointX, $ pointY, $ pointColor) ;}@ imagestring ($ im, 5, 5, 1, $ randval, $ stringColor); $ ImageFun = 'image '. $ type; $ ImageFun ($ im); @ imagedestroy ($ im); $ _ SESSION ['checkcode'] = $ randval; function randStr ($ len = 6, $ format = 'all') {switch ($ format) {case 'all': // Generate a verification code containing digits and letters $ chars = 'callback'; break; case 'char ': // only generate the verification code containing letters $ chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; break; case 'number ': // Generate a verification code containing only numbers $ chars = '20160301'; break; default: $ chars = '2016'; break;} $ string = ''; while (strlen ($ string) <$ len) $ string. = substr ($ chars, (mt_rand () % strlen ($ chars), 1); return $ string ;}
For details about how to use this function, see the following example (here is the verification code for generating full numbers ):
The above is the implementation function for php to generate the verification code. I hope it will be helpful for you to learn.