This article mainly introduces the implementation code of the php digital verification code, which has some reference value. if you need it, it is very convenient to refer to implementing the verification code in php, the key point is to master the usage of the php gd library and session.
Looking at the Online php verification code generation example, there is no combination of php gd library and session, and php is used to generate a random number.
PHP verification codes can be divided into many types, includingPhp image verification code, php random verification code,AndPhp Chinese verification codeAnd use different verification codes based on different application scenarios.
Here is a php digital verification code for your reference.
4-digit verification code
/** Filename: authpage. php */session_start (); // srand (double) microtime () * 1000000); $ authnum = $ _ SESSION ['authnum']; // verify whether the user input is consistent with the verification code if (isset ($ _ POST ['authinput']) {if (strcmp ($ _ POST ['authinput'], $ _ SESSION ['authnum']) = 0) echo "verification successful! "; Else echo" verification failed! ";}/// Generate a new four-digit integer verification code // while ($ authnum = rand () % 10000) <1000);?>
Authimg. php
<? Php // Generate the verification code image Header ("Content-type: image/PNG"); srand (double) microtime () * 1000000 ); // play the seeds that generate random numbers to facilitate the use of session_start () for the following random number generation; // Save the random number to the session $ _ SESSION ['authnum'] = ""; $ im = imagecreate (); // specify the image background size $ black = ImageColorAllocate ($ im, 0); // set three colors $ white = ImageColorAllocate ($ im, 255,255,255); $ gray = ImageColorAllocate ($ im, 200,200,200); imagefill ($ im, $ gray); // use the area filling method, set (0, 0) while ($ authnum = rand () % 100000) <10000); // map the four-digit integer verification code to the image $ _ SESSION ['authnum'] = $ authnum; imagestring ($ im, 3, $ authnum, $ black); // use the col color to draw the string s to the x and y coordinates of the image (0, 0 in the upper left corner of the image ). // If the font is 1, 2, 3, 4, or 5, use the built-in font for ($ I = 0; $ I <200; $ 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);?>
The above is the implementation code for php to generate a 4-digit verification code. I hope it will help you learn and be able to master php verification code more skillfully.