How to Use the php gd library to generate a verification code

Source: Internet
Author: User

When we want to useAdd a line of reference in php. ini: extension = php_gd2.dll

Restart apache. Make a test page var_dump (gd_info (). The output data indicates that the php gd library has been referenced successfully.

Table ticket auth.html

 
 
  1. <Html> 
  2. <Head> 
  3. <Meta Http-equiv='Content-type' Content='Text/html; charset = UTF-8'> 
  4. <Title>Verification Code</Title> 
  5. </Head> 
  6. <Body> 
  7. <H1>Enter Verification Code</H1> 
  8. <Form Action="Check_auth.php" Method="Post"> 
  9. <Input Name="Auth" Type="Text"> 
  10. <Img Src="Auth. php" Border="0" /> 
  11. <Input Type="Submit" Value="Submit"> 
  12. </Form> 
  13. </Body> 
  14. </Html> 

Php gd library to generate Verification Code auth. php

 
 
  1. <?Php 
  2. Session_start ();
  3. Header ("Content-type: image/png ");
  4.  
  5. $Img_width=100;
  6. $Img_height=20;
  7.  
  8. Srand (microtime () * 100000 );
  9. For ($I=0; $ I<4; $ I ++)
  10. {
  11. $New_number.=Dechex(Rand (0, 15 ));
  12. }
  13.  
  14. $ _ SESSION [check_auth] = $ new_number;
  15. $New_number=ImageCreate($ Img_width, $ img_height); // create an image
  16. ImageColorAllocate ($ new_number, 255,255,255); // set the background color to white.
  17.  
  18. For ($I=0; $ I<Strlen($ _ SESSION [check_auth]); $ I ++)
  19. {
  20. $Font=Mt_rand(3, 5 );
  21. $X=Mt_rand(1, 8) + $ img_width * $ I/4;
  22. $Y=Mt_rand(1, $ img_height/4 );
  23. $Color=ImageColorAllocate($ New_number, mt_rand (0,100), mt_rand (0,150), mt_rand (0,200); // set the Character Color
  24. ImageString ($ new_number, $ font, $ x, $ y, $ _ SESSION [check_auth] [$ I], $ color); // output character
  25. }
  26.  
  27. ImagePng ($ new_number );
  28. ImageDestroy ($ new_number );
  29. ?> 

Php gd library submission page check_auth.php

 
 
  1. <?Php 
  2. Session_start ();
  3. $Auth= $ _ POST ['auth '];
  4.  
  5. If (empty ($ auth ))
  6. {
  7. Echo 'error: The Verification Code cannot be blank ';
  8. Die;
  9. }
  10.  
  11. If ($Auth==$ _ SESSION ['check _ auth'])
  12. {
  13. Echo 'true ';
  14. }
  15. Else
  16. {
  17. Echo 'error: Incorrect verification code input ';
  18. }
  19. ?> 

The above is the knowledge about generating verification codes in the php gd library described in this article. I hope it will be helpful to 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.