Generate verification code and randomly interfere with php implementation

Source: Internet
Author: User
Generate verification code and randomly interfere with php implementation

  1. Session_start ();
  2. Header ('content-type: image/png ');
  3. // Create an image
  4. $ Im = imagecreate ($ x = 130, $ y = 45 );
  5. $ Bg = imagecolorallocate ($ im, rand (50,200), rand (0,155), rand (0,155); // The first time on imagecolorallocate () will fill the background color for the image based on the color palette.
  6. $ FontColor = imageColorAllocate ($ im, 255,255,255); // font color
  7. $ Fontstyle = 'rock. ttf'; // the font style, which can be found in the c: \ windows \ Fonts \ folder. I put it in the and authcode. the PHP file is in the same directory. other font styles can be replaced here.
  8. // Generate random characters
  9. For ($ I = 0; $ I <4; $ I ++ ){
  10. $ RandAsciiNumArray = array (rand (48, 57), rand (65, 90 ));
  11. $ RandAsciiNum = $ randAsciiNumArray [rand (0, 1)];
  12. $ RandStr = chr ($ randAsciiNum );
  13. Imagettftext ($ im, 30, rand ()-rand (), 5 + $ I * 30, rand (), $ fontColor, $ fontstyle, $ randStr );
  14. $ Authcode. = $ randStr;
  15. }
  16. $ _ SESSION ['authcode'] = $ randFourStr; // compare the verification code entered by the user with that entered by the user
  17. // Interference line
  18. For ($ I = 0; $ I <8; $ I ++ ){
  19. $ LineColor = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
  20. Imageline ($ im, rand (0, $ x), 0, rand (0, $ x), $ y, $ lineColor );
  21. }
  22. // Interference point
  23. For ($ I = 0; I I <250; $ I ++ ){
  24. Imagesetpixel ($ im, rand (0, $ x), rand (0, $ y), $ fontColor );
  25. }
  26. Imagepng ($ im );
  27. Imagedestroy ($ im );
  28. ?>


Verification code, php

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.