Application Analysis of PHP verification code on the verification page

Source: Internet
Author: User

For a website, a page that provides verification is required. Now we need to consider the verification code application on the page. Next we will give you a detailed explanation.1. Prepare a page for displaying and submitting the PHP Verification Code

 
 
  1. <?Php
  2. @ Header ("content-type: text/html;Charset=UTF-8 ");
  3. // Open the 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="100" Width=300 Src="Img. php" Frameborder="0" ></Iframe>
  14. <Br/>
  15. <Input Type=Button Value="Cannot see clearly, 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>

2. The following is the PHP verification code generation page, which is called by 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 an 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 the PHP verification code to the session
  17. Session_register ($ randval );
  18. // The following figure shows the verification code.
  19. $Height=50; // Image Height
  20. $Width=100; // Graph width
  21. $Im=ImageCreateTrueColor($ Width, $ height );
  22. $White=ImageColorAllocate($ Instant, 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, 16, mt_rand (0, $ width-strlen ($ randval) * 10), mt_rand (0, $ height-12), $ randval, $ blue );
  31. ImageGIF ($ im );
  32. ImageDestroy ($ im );
  33. /*
  34. Note: To support the above plot functions, we must load the GD2 graph processing library in PHP to modify
  35. ;Extension=Php_gd2. Dll
  36. Is
  37. Extension=Php_gd2. Dll
  38. Enable the GD2 Library
  39. */
  40. ?>

3. This is the verification page, prompting whether the PHP Verification Code has passed verification.

 
 
  1. <?Php@ Header ("content-type: text/html;Charset=UTF-8 ");
  2. // Enable session
  3. Session_start ();
  4. // Obtain the verification code entered by the user and convert it to uppercase.
  5. $ImgId_req= $ _ REQUEST ['imgid'];
  6. $ImgId_req=Strtoupper($ ImgId_req );
  7. // Verify whether the session variable is registered for the string
  8. If (session_is_registered ($ imgId_req )){
  9. Echo"<Font Color=Blue >Verification passed!</Font>";
  10. } Else {
  11. Echo"<Font Color=Red >Verification error!</Font>";
  12. }
  13. // Close the session to clear all registered Variables
  14. Session_destroy ();
  15. ?>

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.