Php code for generating a dynamic image verification code

Source: Internet
Author: User
Php code for generating a dynamic image verification code

  1. Session_start ();
  2. Function random ($ len)
  3. {
  4. $ SrCStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
  5. $ Strs = "";
  6. For ($ I = 0; $ I <$ len; $ I ++)
  7. {
  8. $ Strs. = $ srCStr [mt_rand (0, 35)];
  9. }
  10. Return $ strs;
  11. }
  12. $ Str = random (4); // randomly generated string
  13. $ Width = 50; // the image width of the verification code
  14. $ Height = 25; // height of the verification code Image
  15. @ Header ("Content-Type: image/png ");
  16. $ _ SESSION ["code"] = $ str;
  17. $ Im = imagecreate ($ width, $ height );
  18. // Background color
  19. $ Back = imagecolorallocate ($ im, 0xFF, 0xFF, 0xFF );
  20. // Blur the vertex color
  21. $ Pix = imagecolorallocate ($ im, 187,230,247 );
  22. // Font color
  23. $ Font = imagecolorallocate ($ im, 41,163,238 );
  24. // Plot the vertices of fuzzy match.
  25. For ($ I = 0; I I <1000; $ I ++)
  26. {
  27. Imagesetpixel ($ im, mt_rand (0, $ width), mt_rand (0, $ height), $ pix );
  28. }
  29. Imagestring ($ im, 5, 7, 5, $ str, $ font );
  30. Imagerectangle ($ im, 0, 0, $ width-1, $ height-1, $ font );
  31. Imagepng ($ im );
  32. Imagedestroy ($ im );
  33. $ _ SESSION ["code"] = $ str;
  34. ?>

Summary: from PHP 4.2.0, the random number generator is no longer needed to be planted using the srand () or mt_srand () function, which is now automatically completed. Articles you may be interested in: php generates N non-repeated random numbers php randomly generates a 4-digit verification code php randomly displays an image function php randomly displays an image example php generates a random password example generate a random user name and password php the function is used to generate random user names in batches. The php program uses php to generate random numbers randomly. the Function uses PHP to generate a random number. The php code generates a random number. example php generate random string function php random password generation function php generate random password function PHP generate random string function php generate random password several methods to generate random password

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.