Php uses the verification code to prevent malicious registration of study notes

Source: Internet
Author: User
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:

  1. // Generate a 4-digit verification code randomly
  2. $ Num = ""; for ($ I = 0; $ I <4; $ I ++) {$ num. = rand (0, 9 );}
  3. // The four-digit verification code can also be directly generated using rand (,)
  4. // Write the generated verification code to the session, which is used on the standby verification page.
  5. Session_start (); $ _ SESSION ["Checknum"] = $ num;
  6. // Create an image and define the color value Header ("Content-type: image/PNG ");
  7. Srand (double) microtime () * 1000000 );
  8. $ Im = imagecreate (60, 20 );
  9. $ Black = ImageColorAllocate ($ im, 0, 0 );
  10. $ Gray = ImageColorAllocate ($ im, 200,200,200 );
  11. Imagefill ($ im, 0, 0, $ gray );
  12. // Randomly draw two dotted lines to interfere
  13. $ Style = array ($ black, $ gray, $ gray );
  14. Imagesetstyle ($ im, $ style );
  15. $ Y1 = rand (); $ y2 = rand (); $ y3 = rand (); $ y4 = rand );
  16. Imageline ($ im, 0, $ y1, 60, $ y3, IMG_COLOR_STYLED );
  17. Imageline ($ im, 0, $ y2, 60, $ y4, IMG_COLOR_STYLED)
  18. // Randomly generate a large number of black spots on the canvas, which can interfere with the canvas;
  19. For ($ I = 0; $ I <80; $ I ++ ){
  20. Imagesetpixel ($ im, rand (0, 60), rand (0, 20), $ black );}
  21. // 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.
  22. $ Strx = rand (3, 8 );
  23. For ($ I = 0; $ I <4; $ I ++ ){
  24. $ Strpos = rand (1, 6); imagestring ($ im, 5, $ strx, $ strpos, substr ($ num, $ I, 1), $ black ); $ strx + = rand (8, 12 );
  25. }
  26. ImagePNG ($ im); ImageDestroy ($ im );
  27. ?>

On the reg. php page, write a form: (other HTML code is saved here). The code is as follows:

  1.  
  2. Verification code: 
  3.  
  4.  
  5.  
  6. Verification code: 
  7.  

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:

  1. Session_start ();
  2. // The error message is displayed when the back_alert () verification code is incorrect.
  3. Function back_alert ($ yzm ){
  4. Echo" Alert ('$ yzm'); history. back (); script ";
  5. }
  6. // Prohibit malicious calls (prohibit direct access to the post. php page in the browser)
  7. If ($ _ POST ["yzm"] = null ){
  8. Back_alert ??? ');}
  9. // Disable malicious registration
  10. If (! ($ _ POST ["yzm"] ==$ _ SESSION ["Checknum"]) {
  11. Back_alert ('incorrect verification code ');
  12. } Echo $ _ POST ["yzm"];

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.