Php verification code

Source: Internet
Author: User
Php verification code
PHP verification code implementation principle

Generate a random number or letter and save it to the session (used to verify the verification code). then plot the generated number or letter! And then present it to us.

Refresh verification code: use js to change the parameters of the verification code image so that the browser does not read the cached image to refresh the verification code!

Sample code

  1. $ Str = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890 ";
  2. $ Image = imagecreate (50, 25 );
  3. Imagecolorallocate ($ image, mt_rand (0,125), mt_rand (0,125), mt_rand (0,125 ));
  4. $ Color = imagecolorallocate ($ image, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
  5. For ($ I = 1; $ I <= 4; $ I ++ ){
  6. $ Date = $ str [mt_rand (0, strlen ($ str)-1)];
  7. $ Code. = $ date;
  8. }
  9. Session_start ();
  10. $ _ SESSION ['code'] = $ code;
  11. Imagestring ($ image, 4,8, 4, $ code, $ color );
  12. For ($ I = 1; $ I <= 30; $ I ++ ){
  13. Imagesetpixel ($ image, mt_rand (125,200), mt_rand (), mt_rand ));
  14. }
  15. For ($ I = 1; $ I <= mt_rand (); $ I ++) {imageline ($ image, mt_rand (), mt_rand ), mt_rand (100,150), mt_rand (), mt_rand ();} header ("content-type: image/png"); imagepng ($ image);?>
  16. Digit + letter verification code (different letters in different colors ):
  17. $ Str = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890 ";
  18. $ Image = imagecreate (50, 25 );
  19. Imagecolorallocate ($ image, mt_rand (0,125), mt_rand (0,125), mt_rand (0,125 ));
  20. $ Color [0] = imagecolorallocate ($ image, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
  21. $ Color [1] = imagecolorallocate ($ image, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
  22. $ Color [2] = imagecolorallocate ($ image, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
  23. $ Color [3] = imagecolorallocate ($ image, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
  24. For ($ I = 0; $ I <4; $ I ++ ){
  25. $ Date = $ str [mt_rand (0, strlen ($ str)-1)];
  26. $ Code. = $ date;
  27. Imagestring ($ image, 5, 6 + $ I * 10, 4, $ code [$ I], $ color [$ I]);
  28. }
  29. Session_start ();
  30. $ _ SESSION ['code'] = $ code;
  31. For ($ I = 1; $ I <= 30; $ I ++ ){
  32. Imagesetpixel ($ image, mt_rand (125,200), mt_rand (), mt_rand ));
  33. }
  34. For ($ I = 1; $ I <= mt_rand (1, 5); $ I ++ ){
  35. Imageline ($ image, mt_rand (100,150), mt_rand ));
  36. }
  37. Header ("content-type: image/png ");
  38. Imagepng ($ image );

From: php verification code implementation principle

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.