The common method to prevent malicious registration is to use the verification code. when the user submits the registration information, I randomly generate a graphic verification code so that only one person can recognize it. of course, the simple verification code machine is
The common method to prevent malicious registration is to use the verification code. when a user submits the registration information, I randomly generate a graphic verification code so that only one person can recognize it, of course, simple verification code machines are identified by machines, so the complexity is better.
Today, we will study the PHP verification code. we will implement it through a simple digital verification code. First, we will write a code to generate the verification code:
-
- // Generate a 4-digit verification code randomly
- $ Num = ""; for ($ I = 0; $ I <4; $ I ++) {$ num. = rand (0, 9 );}
- // The four-digit verification code can also be directly generated using rand (,)
- // Write the generated verification code to the session, which is used on the standby verification page.
- Session_start (); $ _ SESSION ["Checknum"] = $ num;
- // Create an image and define the color value Header ("Content-type: image/PNG ");
- Srand (double) microtime () * 1000000 );
- $ Im = imagecreate (60, 20 );
- $ Black = ImageColorAllocate ($ im, 0, 0 );
- $ Gray = ImageColorAllocate ($ im, 200,200,200 );
- Imagefill ($ im, 0, 0, $ gray );
- // Randomly draw two dotted lines to interfere
- $ Style = array ($ black, $ gray, $ gray );
- Imagesetstyle ($ im, $ style );
- $ Y1 = rand (); $ y2 = rand (); $ y3 = rand (); $ y4 = rand );
- Imageline ($ im, 0, $ y1, 60, $ y3, IMG_COLOR_STYLED );
- Imageline ($ im, 0, $ y2, 60, $ y4, IMG_COLOR_STYLED)
- // Randomly generate a large number of black spots on the canvas, which can interfere with the canvas;
- For ($ I = 0; $ I <80; $ I ++ ){
- Imagesetpixel ($ im, rand (0, 60), rand (0, 20), $ black );}
- // Display the four numbers randomly on the canvas. the horizontal spacing and position of the characters are randomly generated according to a certain fluctuation range.
- $ Strx = rand (3, 8 );
- For ($ I = 0; $ I <4; $ I ++ ){
- $ Strpos = rand (1, 6); imagestring ($ im, 5, $ strx, $ strpos, substr ($ num, $ I, 1), $ black ); $ strx + = rand (8, 12 );
- }
- ImagePNG ($ im); ImageDestroy ($ im );
- ?>
On the reg. php page, write a form: (other HTML code is saved here). The code is as follows:
-
- Verification code:
-
-
-
- Verification code:
-
Because we use post for submission, we use $ _ POST to obtain the code (verify the verification code on the acceptance page: post. php page) as follows:
- Session_start ();
- // The error message is displayed when the back_alert () verification code is incorrect.
- Function back_alert ($ yzm ){
- Echo" Alert ('$ yzm'); history. back (); script ";
- }
- // Prohibit malicious calls (prohibit direct access to the post. php page in the browser)
- If ($ _ POST ["yzm"] = null ){
- Back_alert ??? ');}
- // Disable malicious registration
- If (! ($ _ POST ["yzm"] ==$ _ SESSION ["Checknum"]) {
- Back_alert ('incorrect verification code ');
- } Echo $ _ POST ["yzm"];