How to use PHP GD library to generate verification code _php tutorial

Source: Internet
Author: User
When we want to useAdd a line to the php.ini first: Extension=php_gd2.dll

Restart Apache. Make a test page var_dump (Gd_info ()); The output data indicates that the PHP GD Library Reference was successful.

Form 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 > Please enter a 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="commit">
  12. form>
  13. body>
  14. html>

PHP GD Library generated verification code auth.php

 
 
  1. php
  2. Session_Start ();
  3. Header ("Content-type:image/png");
  4. $ Img_width = - ;
  5. $ Img_height = - ;
  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 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 empty ';
  7. Die
  8. }
  9. if ($auth==$_session[' Check_auth '))
  10. {
  11. Echo ' right ';
  12. }
  13. Else
  14. {
  15. Echo ' ERROR: Verification code input error ';
  16. }
  17. ?>

The above is the introduction of the PHP GD library generated verification code related knowledge, we hope to help.


http://www.bkjia.com/PHPjc/446427.html www.bkjia.com true http://www.bkjia.com/PHPjc/446427.html techarticle when we want to use first add a line 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 Reference was successful. 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.