How PHP implements the verification code

Source: Internet
Author: User

Now, let's talk about simple, pure digital verification codes.

For beginners, it is recommended to follow the comments//numbers of my code. The simplest way to do this is to copy the entire code away.

Create a new captcha.php:

<?php//11> Set session, must be at the top of the script session_start ();    /* $image = Imagecreatetruecolor (100, 30);  1> set captcha picture size function//5> set CAPTCHA color imagecolorallocate (int im, int red, int green, int blue); $bgcolor = Imagecolorallocate ($image, 255,255,255); #ffffff the area of the//6> area filled with int imagefill (int im, int x, int y, int col) (x/y) is shaded, col represents the color to be painted Imagefill ($image, 0, 0, $b  Gcolor);        10> Set Variable $captcha _code = ""; *///7> Generate random numbers for ($i =0; $i <4; $i + +) {//set font size $fontsize = 6;      Set the font color, random color $fontcolor = imagecolorallocate ($image, Rand (0,120), Rand (0,120), Rand (0,120));    0-120 Dark Color//Set Number $fontcontent = rand (0,9);      10>.= continuously defines the variable $captcha _code. = $fontcontent;    Set coordinates $x = ($i *100/4) +rand (5,10);     $y = rand (5,10);  Imagestring ($image, $fontsize, $x, $y, $fontcontent, $fontcolor);  }//10> save to SESSION $_session[' authcode ' = $captcha _code; 8> add interference element, set Snowflake point for ($i =0; $i <200; $i + +) {//Set point color, 50-200 color is lighter than digital, do not disturb reading $pointcolor = ImagecoLorallocate ($image, Rand (50,200), Rand (50,200), Rand (50,200));  Imagesetpixel-draws a single pixel Imagesetpixel ($image, Rand (1,99), Rand (1,29), $pointcolor); }//9> Add interference element, set horizontal for ($i =0; $i <4; $i + +) {//set line Color $linecolor = Imagecolorallocate ($image, Rand (80,220), rand (80    , (+), rand (80,220));  Set line, 2.1-wire imageline ($image, Rand (1,99), Rand (1,29), Rand (1,99), Rand (1,29), $linecolor);  }//2> set head, image/png header (' content-type:image/png ');  3>imagepng () establishes the PNG graph function imagepng ($image); 4>imagedestroy () End graph function Destroy $image Imagedestroy ($image);

Then there is the code for the static page: index.html

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

From index.html you can see that the submitted form is to form.php, so there is a form.php code to judge:

<?php  Header ("Content-type:text/html;charset=utf-8");      Set Header information  //isset () detects if the variable is set  if (Isset ($_request[' Authcode '))) {    session_start ();    Strtolower () lowercase function    if (strtolower ($_request[' authcode ") = = $_session[' Authcode ']) {      //jump page      echo" < Script language=\ "javascript\" > ";      echo "document.location=\"./form.php\ "";      echo "</script>";    } else{      //Tips and jump page      echo "<script language=\" javascript\ ">";      echo "Alert (' Input error! ');";      echo "document.location=\"./form.php\ "";      echo "</script>";    }    Exit ();  }

The display page is as follows:

Digital plus English Verification code, just change the captcha.php page 7 ", the other two pages do not need to move, the code is as follows:

<?php//11> Set session, must be at the top of the script session_start ();    $image = Imagecreatetruecolor (100, 30);  1> set captcha picture size function//5> set CAPTCHA color imagecolorallocate (int im, int red, int green, int blue); $bgcolor = Imagecolorallocate ($image, 255,255,255); #ffffff the area of the//6> area filled with int imagefill (int im, int x, int y, int col) (x/y) is shaded, col represents the color to be painted Imagefill ($image, 0, 0, $b  Gcolor);        10> Set Variable $captcha _code = "";//7> generate random letters and numbers for ($i =0; $i <4; $i + +) {//set font size $fontsize = 8;      Set the font color, random color $fontcolor = imagecolorallocate ($image, Rand (0,120), Rand (0,120), Rand (0,120));    0-120 Dark Color//Set the values that need to be randomly taken, removing the error-prone values such as 0 and o $data = ' abcdefghigkmnpqrstuvwxy3456789 ';    Take out the value, String intercept method strlen Gets the string length $fontcontent = substr ($data, Rand (0,strlen ($DATA)), 1);        10>.= continuously defines the variable $captcha _code. = $fontcontent;    Set coordinates $x = ($i *100/4) +rand (5,10);    $y = rand (5,10);  Imagestring ($image, $fontsize, $x, $y, $fontcontent, $fontcolor); }//10> save to SESSION $_session[' authcode ' = $captCha_code; 8> add interference element, set Snowflake point for ($i =0; $i <200; $i + +) {//Set point color, 50-200 color is lighter than digital, do not disturb reading $pointcolor = Imagecolorallocate ($image, R        and (50,200), Rand (50,200), Rand (50,200));  Imagesetpixel-draws a single pixel Imagesetpixel ($image, Rand (1,99), Rand (1,29), $pointcolor); }//9> Add interference element, set horizontal for ($i =0; $i <4; $i + +) {//set line Color $linecolor = Imagecolorallocate ($image, Rand (80,220), rand (80    , (+), rand (80,220));  Set line, 2.1-wire imageline ($image, Rand (1,99), Rand (1,29), Rand (1,99), Rand (1,29), $linecolor);  }//2> set head, image/png header (' content-type:image/png ');  3>imagepng () establishes the PNG graph function imagepng ($image); 4>imagedestroy () End graph function Destroy $image Imagedestroy ($image);

  

How PHP implements the verification code

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.