Example of a PHP digital verification code

Source: Internet
Author: User

It is very convenient to implement 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, including php image verification codes, php Random verification codes, and php Chinese verification codes. Different verification codes are used for different application scenarios.

Here is a php digital verification code for your reference.


1. Digital Verification Code
  1. /*
  2. * Filename: authpage. php
  3. */
  4. Session_start ();
  5. // Srand (double) microtime () * 1000000 );
  6. $ Authnum = $ _ SESSION ['authnum'];
  7. // Verify that the user input is consistent with the verification code
  8. If (isset ($ _ POST ['authinput'])
  9. {
  10. If (strcmp ($ _ POST ['authinput'], $ _ SESSION ['authnum']) = 0)
  11. Echo "Verification Successful! ";
  12. Else
  13. Echo "Verification Failed! ";
  14. }
  15. // Generate a new four-digit integer Verification Code
  16. // While ($ authnum = rand () % 10000) <1000 );
  17. ?>
  18. <Formaction = test4.phpmethod = post>
  19. <Table>
  20. Enter the verification code: <inputtype = textname = authinputstyle = "width: 80px"> <br>
  21. <Inputtype = submitname = "verify" value = "Submit verification code">
  22. <Inputtype = hiddenname = authnumvalue = <? Echo $ authnum;?>
  23. </Table>
  24. </Form>
2, authimg. php
  1. <? Php
  2. // Generate a verification code Image
  3. Header ("Content-type: image/PNG ");
  4. Srand (double) microtime () * 1000000); // play the next seed that generates random numbers to facilitate the use of the following Random Number Generation
  5. Session_start (); // Save the random number to the session.
  6. $ _ SESSION ['authnum'] = "";
  7. $ Im = imagecreate (); // specify the image background size.
  8. $ Black = ImageColorAllocate ($ im, 0, 0); // you can specify three colors.
  9. $ White = ImageColorAllocate ($ im, 255,255,255 );
  10. $ Gray = ImageColorAllocate ($ im, 200,200,200 );
  11. Imagefill ($ im, $ gray); // use the area filling method, set)
  12. While ($ authnum = rand () % 100000) <10000 );
  13. // Print the four-digit integer verification code into the image
  14. // Www.jbxue.com
  15. $ _ SESSION ['authnum'] = $ authnum;
  16. Imagestring ($ im, 5, 10, 3, $ authnum, $ black );
  17. // 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 ).
  18. // If the font is 1, 2, 3, 4, or 5, use the built-in font.
  19. For ($ I = 0; $ I <200; $ I ++) // Add interference pixels
  20. {
  21. $ Randcolor = ImageColorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
  22. Imagesetpixel ($ im, rand () % 70, rand () % 30, $ randcolor );
  23. }
  24. ImagePNG ($ im );
  25. ImageDestroy ($ im );
  26. ?>

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.