PHP Verification code in the verification page of the application Analysis _php tutorial

Source: Internet
Author: User
Tags register php
For a website, you will definitely need a page that provides validation functionality. Then we need to consider the application of the verification code in the verification page now. We're going to give you a detailed explanation of the prepare a page to display and submit a PHP verification code

 
 
  1. Php
  2. @header ("content-type:text/html; CharSet = UTF -8 ");
  3. Open session
  4. Session_Start ();
  5. ?>
  6. < HTML >
  7. < Head >
  8. < Meta http-equiv="Content-type" Content="text/ html Charset=utf-8 " />
  9. < title > PHP Verification code example title>
  10. Head >
  11. < Body >
  12. Verification Code: <br/>
  13. < iframe id="iimg" height=" width = - src="img.php" frameborder="0" > IFrame >
  14. < BR />
  15. < input type= button value="Can't see, change one" onclick = "iimg.location.reload ();" >
  16. < BR >
  17. < form action="validate.php" method="POST" >
  18. Enter the verification code: <input name="Imgid" style="width:60" >
  19. < input type="Submit" value="OK">
  20. form >
  21. Body >
  22. HTML >

Second, the following is the PHP code generation page, the page is called on the first page

 
 
  1. Php
  2. Header ("Content-type:image/gif");
  3. Session_Start ();
  4. The verification code is a random character, the following is the algorithm
  5. $randval;
  6. For ($i=0; $i<7; $i + +) {
  7. $ Randstr = Mt_rand (Ord (' A '), Ord (' Z '));
  8. Srand (Double) microtime () *1000000);
  9. $ RANDV = Mt_rand (0,10);
  10. if ($randv%2==0) {
  11. $ Randval. = Mt_rand (0,10);
  12. }else{
  13. $ Randval. = CHR ($RANDSTR);
  14. }
  15. }
  16. Register PHP verification code to session
  17. Session_register ($randval);
  18. Here is the drawing of the Verification Code map
  19. $ Height = - ;//Graph High
  20. $ width = - ;//Graph width
  21. $ im = Imagecreatetruecolor ($width, $height);
  22. $ White = imagecolorallocate ($im, 255, 255, 255);
  23. $ Blue = imagecolorallocate ($im, 0, 0, 64);
  24. Imagefill ($im, 0, 0, $white);
  25. Srand (Double) microtime () *1000000000);
  26. Imageline ($im, Mt_rand (0, $width/3), Mt_rand (0, $height/3), Mt_rand ($width/3, $width), Mt_rand ($height/3, $height), $ Blue);
  27. Srand (Double) microtime () *1000000);
  28. Imageline ($im, Mt_rand ($width/3, $width), Mt_rand (0, $height/3), Mt_rand (0, $width/3), Mt_rand (0, $height/3), $blue);
  29. Srand (Double) microtime () *1000000);
  30. Imagestring ($im, Mt_rand (0, $width-strlen ($randval) *), Mt_rand (0, $height -12), $randval, $blue);
  31. Imagegif ($im);
  32. Imagedestroy ($im);
  33. /*
  34. It is important to note that in order to support the above drawing functions, we must load the GD2 graphics processing library in PHP, which can be modified in the php.ini file.
  35. ; extension = PHP_GD2 . DLL
  36. For
  37. extension = PHP_GD2 . DLL
  38. That opens the GD2 library.
  39. */
  40. ?>

Third, this is the verification page, indicating whether PHP verification code through verification

 
 
  1. php @header ("content-type:text/html; CharSet = UTF -8 ");
  2. Open session
  3. Session_Start ();
  4. Get user-entered verification code and convert to uppercase
  5. $ Imgid_req = $_request[' Imgid '];
  6. $ Imgid_req = Strtoupper ($imgId _req);
  7. Verify that the string is registered with the session variable
  8. if (session_is_registered ($imgId _req)) {
  9. echo " <font color= blue > verified! font> ";
  10. } else {
  11. echo " <font color= red > validation Error! font> ";
  12. }
  13. Close session to clear all registered variables
  14. Session_destroy ();
  15. ?>

http://www.bkjia.com/PHPjc/446325.html www.bkjia.com true http://www.bkjia.com/PHPjc/446325.html techarticle for a website, you will definitely need a page that provides validation functionality. Then we need to consider the application of the verification code in the verification page now. We're going to be here for everyone .

  • Related Article

    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.