Php random generation of image verification code

Source: Internet
Author: User
Php random generation of image verification code

  1. Session_start ();
  2. // A string that randomly generates the verification code
  3. Function random ($ len ){
  4. $ Srcstr = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789 ";
  5. Mt_srand ();
  6. $ Strs = "";
  7. For ($ I = 0; $ I <$ len; $ I ++ ){
  8. $ Strs. = $ srcstr [mt_rand (0, 33)];
  9. }
  10. Return ($ strs );
  11. }
  12. $ Str = random (5); // randomly generated string
  13. $ Width = 60; // the image width of the verification code
  14. $ Height = 25; // height of the verification code Image
  15. // Date in the past
  16. Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT ");
  17. // Always modified
  18. Header ("Last-Modified:". gmdate ("D, d m y h: I: s"). "GMT ");
  19. // HTTP/1.1
  20. Header ("Cache-Control: no-store, no-cache, must-revalidate ");
  21. Header ("Cache-Control: post-check = 0, pre-check = 0", false );
  22. // HTTP/1.0
  23. Header ("Pragma: no-cache ");
  24. Header ("Content-Type: image/png ");
  25. $ Im = imagecreate ($ width, $ height );
  26. $ Back = imagecolorallocate ($ im, 0xFF, 0xFF, 0xFF); // background color
  27. $ Pix = imagecolorallocate ($ im, 187,190,247); // blur the dot color
  28. $ Font = imagecolorallocate ($ im, 41,163,238); // font color
  29. // Draw 1000 blur points
  30. Mt_srand ();
  31. For ($ I = 0; I I <1000; $ I ++ ){
  32. Imagesetpixel ($ im, mt_rand (0, $ width), mt_rand (0, $ height), $ pix );
  33. }
  34. Imagestring ($ im, 5, 7, 5, $ str, $ font); // draw a random string
  35. Imagerectangle ($ im, $ width-1, $ height-1, $ font); // draw a 1px border around the verification code Image
  36. Imagepng ($ im); // Create a PNG image
  37. Imagedestroy ($ im); // deconstruct the image handle and release it to the memory space.
  38. $ _ SESSION ["auth_code"] = $ str;
  39. ?>

Example 2: php random verification code php generates a simple verification code

  1. $ Image_width = 140;
  2. $ Image_height = 50;
  3. Srand (microtime () * 10000 );
  4. For ($ I = 0; $ I <4; $ I ++ ){
  5. $ Number. = dechex (rand (0, 15 ));
  6. }
  7. $ Check = $ number;
  8. $ Im = imagecreate ($ image_width, $ image_height );
  9. Imagecolorallocate ($ im, 255,255,255 );
  10. For ($ I = 0; $ I
  11. $ Font = rand (40, 80 );
  12. $ X = rand (1, 8) + $ image_width * $ I/4;
  13. $ Y = rand (1, $ image_height/4 );
  14. $ Color = imagecolorallocate ($ im, rand (0,100), rand (0,150), rand (0,200 ));
  15. Imagestring ($ im, $ font, $ x, $ y, $ check [$ I], $ color );
  16. }
  17. Imagepng ($ im );
  18. Imagedestroy ($ im );
  19. ?>

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.