Teach you how PHP implements verification code _php instances

Source: Internet
Author: User
Verification code in the form implementation more and more, but with JS write verification code, always feel inconvenient, so learned the next PHP implementation of the verification code. Well, there is nothing to do, but do not want to waste time, so learned the next PHP implementation verification code. It is the so-called, multi-technology does not pressure body. Also, it can be encapsulated into a function, which is also convenient for later use, but is not encapsulated now.

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//10> 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

 
        
  
       
  
   Confirm Verification Code title>  head>      
  
         
   
    

Verification code: ' Style= ' width:100px; height:30px "/> For a?a> p>

Please enter the verification code:P>

P>

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" ";    } else{      //Hints and jump page      echo "";    }    Exit ();  }

The display page is as follows:

So, the realization of pure numbers, the number of English should not be difficult. The code to be modified is only modified by captcha.php to generate random numbers of//7> to//7> generate random letters and numbers, if you are really cute to modify these words to think can be achieved, then congratulate you, you always keep happy. Children with brain residue are much more happy.

Don't say much nonsense, pull the code.

PHP//10> 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> generates 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);

The other two pages are not allowed to be modified.

In general, it is enough for now. But like the anime, there will always be a pan-out.

Well, let's take a Chinese character out of the pan. In fact, I am also ready to put the verification of Chinese characters into my graduation design, although now very popular sliding verification code, but I am not specifically learning JS.

Moreover, can also and the reply teacher said, our verification code does not need the material, even the picture also produces, uses own knowledge to install 13, also has not set.

PHP//11> set session, must be at the top of the script session_start ();      1> a function that sets the size of the captcha picture $image = Imagecreatetruecolor (200, 60);  5> set the 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); 7> Set TTF Font $fontface = ' fzytk.  TTF '; 7> set the font to achieve a simple digital reserve $str = ' The Heaven and earth is unkind to all things for the dog Saints unkind to the people for the dog this sentence often appear in the tyranny of the tyrant brutality to treat all things as a cheap pig dog to look at and those high above the so-called holy people also do not treat us as a pig dog is not the same thing but is taking the interpretation is not an emotional proven itself to all things Treat the hands of the same as the son of the same time when the protagonist of the hero lovingly looked at each other said the hand of the son and the child old woman tears hazy shy back to a nasty such plot we have not seen a lot but let's take a look at this sentence of the original sentence of the death of the rich and alive said that the hands of the child and the son grow old in sigh, I live Yu Ying no I believe I mean to say the soldiers between the agreement  Said to die together now and I agreed with the people are gone how do I live ah, naked brothers friendship, describe good friends of the base of the love between men and women to fit a lot of it ';    Str_split () cuts a string into an array, a Chinese character in Utf_8 is 3 characters $strdb = Str_split ($str, 3);  >11 $captcha _code = "; 8> generates random man for ($i =0; $i <4; $i + +) {//Set font color, random color $fontcolor = imagecolorallocate ($image, Rand (0,120), Rand (0,120      ), Rand (0,120)); 0-120 Dark Color//randomly selected Chinese $in = rand (0,count ($STRDB));    $CN = $strdb [$in];    Record the Chinese in the string that will be saved to the session $captcha _code. = $CN;  /*imagettftext (Resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, String $text) Curtains, dimensions, angles, coordinates, colors, font paths, text strings Mt_rand () generate better random numbers, four times times faster than rand () */Imagettftext ($image, Mt_rand (20,24), Mt_rand ( -60,60), (40*  $i +20), Mt_rand (30,35), $fontcolor, $fontface, $CN);  }//11> save to SESSION $_session[' authcode ' = $captcha _code; 9> add interference element, set point for ($i =0; $i <200; $i + +) {//Set point color, 50-200 color is lighter than digital, do not disturb reading $pointcolor = Imagecolorallocate ($image, ran        D (50,200), Rand (50,200), Rand (50,200));  Imagesetpixel-draws a single pixel Imagesetpixel ($image, Rand (1,199), Rand (1,59), $pointcolor); }//10> Add interference element, set line 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,199), Rand (1,59), Rand (1,199), Rand (1,59), $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);

As follows

The above is the entire content of this article, to help you to implement PHP digital verification Code, PHP letter verification Code, PHP Chinese character Verification code, I hope that everyone's learning has helped.

  • 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.