PHP generated verification code and Verification Code verification complete instance

Source: Internet
Author: User
    1. Untitled Document
Copy Code
The verifycode.php file code is as follows
  1. /*
  2. Image verification Code Powered by Kason Test http://www.hzhuti.com/nokia/c6/*/
  3. Session_Start ();
  4. $num =4;//number of verification codes
  5. $width =80;//Verification Code width
  6. $height =20;//Verification Code height
  7. $code = ";
  8. for ($i =0; $i < $num; $i + +)//Generate Verification code
  9. {
  10. Switch (rand (0,2))
  11. {
  12. Case 0: $code [$i]=CHR (rand (48,57)); break;//number
  13. Case 1: $code [$i]=CHR (rand (65,90)); break;//Capital Letter
  14. Case 2: $code [$i]=CHR (rand (97,122)); break;//Small Letter
  15. }
  16. }
  17. $_session["Verifycode"]= $code;
  18. $image =imagecreate ($width, $height);
  19. Imagecolorallocate ($image, 255,255,255);
  20. for ($i =0; $i <80; $i + +)//Generate interference pixels
  21. {
  22. $dis _color=imagecolorallocate ($image, Rand (0,2555), Rand (0,255), Rand (0,255));
  23. Imagesetpixel ($image, rand (1, $width), rand (1, $height), $dis _color);
  24. }
  25. for ($i =0; $i < $num; $i + +)//print character to image
  26. {
  27. $char _color=imagecolorallocate ($image, Rand (0,2555), Rand (0,255), Rand (0,255));
  28. Imagechar ($image, Max, ($width/$num) * $i, Rand (0,5), $code [$i], $char _color);
  29. }
  30. Header ("Content-type:image/png");
  31. Imagepng ($image);//output image to browser
  32. Imagedestroy ($image);//Release resources
  33. ?>
Copy Code
checkcode.php files are as follows
    1. Ini_set (' display_errors ', ' Off ');
    2. Session_Start ();
    3. if ((Strtoupper ($_post["code")) ==strtoupper (($_session["Verifycode"])) {
    4. Print ("Verification code is correct,");
    5. }else{
    6. Print ("Captcha error,");
    7. }
    8. echo "submitted Captcha:". Strtoupper ($_post["code"]). ", correct captcha:". Strtoupper ($_session["Verifycode"]);
    9. ?>
Copy Code
Verification Code, PHP This topic was moved by Beckham on 2015-11-18 08:23
  • 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.