PHP graphic verification code implementation method-PHP Tutorial

Source: Internet
Author: User
PHP graphic verification code implementation method. We often see requests for verification codes in website registration and other places. it shows random numbers or symbols in the form of images to users, after verification is provided, we often see the request for verification code in the website registration and other places. it displays random numbers or symbols in the form of images in front of users, and provide verification before using the relevant functions. Today we will introduceThe specific implementation code is as follows:

 
  1. Php
  2. Class rndnum {
  3. Function rnd (){
  4. Srand (double) microtime () * 1000000 );
  5. $Rnd_number=Array(
  6. 1=>'1 ',
  7. 2=>'2 ',
  8. 3=>'3 ',
  9. 4=>'4 ',
  10. 5=>'5 ',
  11. 6=>'6 ',
  12. 7=>'7 ',
  13. 8=>'8 ',
  14. 9=>'9 ',
  15. 10=>'A ',
  16. 11=>'B ',
  17. 12=>'C ',
  18. 13=>'D ',
  19. 14=>'E ',
  20. 15=>'F ',
  21. 16=>'G ',
  22. 17=>'H ',
  23. 18=>'I ',
  24. 19=>'J ',
  25. 20=>'K ',
  26. 21=>'L ',
  27. 22=>'M ',
  28. 23=>'N ',
  29. 24=>'O ',
  30. 25=>'P ',
  31. 26=>'Q ',
  32. 27=>'R ',
  33. 28=>'S ',
  34. 29=>'T ',
  35. 30=>'U ',
  36. 31=>'V ',
  37. 32=>'W ',
  38. 33=>'X ',
  39. 34=>'Y ',
  40. 35=>'Z ',
  41. 36=>'0'
  42. );
  43. $Result=Array_rand($ Rnd_number, 6 );
  44. $J=Count($ Result );
  45. For ($I=0; $ I<$ J; $ I ++ ){
  46. $Re.= $ Rnd_number [$ result [$ I];
  47. }
  48. // $Re= $ Rnd_number [$ result [1]. $ rnd_number [$ result [2]. $ rnd_number [$ result [3]. $ rnd_number [$ result [4]. $ rnd_number [$ result [5]. $ rnd_number [$ result [6]. $ rnd_number [$ result [7];
  49. // Return array_keys ($ result );
  50. Return $ re;
  51. }
  52. }
  53. /* Usage
  54. $Rndnum=NewRndnum ();
  55. $Num= $ Rndnum->Rnd ();
  56. Echo $ num;
  57. */
  58. ?>

Generate an image of the PHP graphic verification code and fill in the random number:

 
  1. Php
  2. Header ("Content-type: image/PNG ");
  3. Require_once ("rndnum. php ");
  4. $Rndnum=NewRndnum ();
  5. $Authnum= $ Rndnum->Rnd ();
  6. Session_start ();
  7. $ _ SESSION ["extrra_code"] = $ authnum;
  8. $Im=Imagecreate(72, 20 );
  9. $Black=ImageColorAllocate($ Im, 0, 0 );
  10. $White=ImageColorAllocate($ Instant, 255,255,255 );
  11. $Gray=ImageColorAllocate($ Instant, 200,200,200 );
  12. Imagefill ($ im, 0, 0, $ gray );
  13. Imagestring ($ im, 5, 10, 3, $ authnum, $ black );
  14. For ($I=0; $ I<200; $ I ++) // add interference pixels
  15. {
  16. $Randcolor=ImageColorallocate($ Im, rand (0,255), rand (0,255), rand (0,255 ));
  17. Imagesetpixel ($ im, rand () % 70, rand () % 30, $ randcolor );
  18. }
  19. ImagePNG ($ im );
  20. ImageDestroy ($ im );
  21. ?>

The above is the implementation code of all PHP graphic verification codes. I hope it will help you.


...

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.