How to use the PHPGD library to generate a verification code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Use the PHPGD library to generate a verification code. Add a reference line in php. ini: extensionphp_gd2.dll to restart apache. Make a test page var_dump (gd_info (). the output data indicates that the PHPGD database has been referenced successfully. Form 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. $Img_width=100;
  5. $Img_height=20;
  6. Srand (microtime () * 100000 );
  7. For ($I=0; $ I<4; $ I ++)
  8. {
  9. $New_number.=Dechex(Rand (0, 15 ));
  10. }
  11. $ _ SESSION [check_auth] = $ new_number;
  12. $New_number=ImageCreate($ Img_width, $ img_height); // create an image
  13. ImageColorAllocate ($ new_number, 255,255,255); // set the background color to white.
  14. For ($I=0; $ I<Strlen($ _ SESSION [check_auth]); $ I ++)
  15. {
  16. $Font=Mt_rand(3, 5 );
  17. $X=Mt_rand(1, 8) + $ img_width * $ I/4;
  18. $Y=Mt_rand(1, $ img_height/4 );
  19. $Color=ImageColorAllocate($ New_number, mt_rand (0,100), mt_rand (0,150), mt_rand (0,200); // set the character color
  20. ImageString ($ new_number, $ font, $ x, $ y, $ _ SESSION [check_auth] [$ I], $ color); // output character
  21. }
  22. ImagePng ($ new_number );
  23. ImageDestroy ($ new_number );
  24. ?>

Php gd Library submission page check_auth.php

 
 
  1. Php
  2. Session_start ();
  3. $Auth= $ _ POST ['auth '];
  4. If (empty ($ auth ))
  5. {
  6. Echo 'Error: the verification code cannot be blank ';
  7. Die;
  8. }
  9. If ($Auth==$ _ SESSION ['Check _ auth'])
  10. {
  11. Echo 'true ';
  12. }
  13. Else
  14. {
  15. Echo 'Error: incorrect verification code input ';
  16. }
  17. ?>

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.


Worker first adds a line of reference in php. ini: extension = php_gd2.dll to restart apache. Make a test page var_dump (gd_info (). the output data indicates that the php gd Library has been referenced successfully. Form...

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.