Home>

Verification codes are increasingly implemented in forms,But the verification code written in js,Always feel inconvenient,So I learned the verification code implemented by PHP.

Well, in fact, there is nothing to do,But do n’t want to waste time,So I learned how to implement verification code in PHP.As the saying goes, the skill is not overwhelming.Moreover, it can also be encapsulated into a function,It is also very convenient for future use.Of course it is not packaged now.

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

If you are a beginner,It is recommended to follow the comment //number of my code step by step.The easiest way,Still 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>Function to set the size of the captcha image
 //5>Set the captcha color imagecolorallocate (int im, int red, int green, int blue);
 $bgcolor=imagecolorallocate ($image, 255,255,255);//#ffffff
 //6>area fill int imagefill (int im, int x, int y, int col) (x, y) where the area is colored,col indicates the color to be painted
 imagefill ($image, 0, 0, $bgcolor);
 //10>set variables
 $captcha_code="";
 //7>Generate random numbers
 for ($i=0;$i<4;$i ++) {
  //Set the 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 the number
  $fontcontent=rand (0,9);
  //10>.=Continuously define variables
  $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>increase interference elements,Set snowflake points
 for ($i=0;$i<200;$i ++) {
  //Set the color of the point,50-200 colors are lighter than numbers,Does not interfere with reading
  $pointcolor=imagecolorallocate ($image, rand (50,200), rand (50,200), rand (50,200));
  //imagesetpixel — draw a single pixel
  imagesetpixel ($image, rand (1,99), rand (1,29), $pointcolor);
 }
 //9>Add interference elements,Set horizontal lines
 for ($i=0;$i<4;$i ++) {
  //Set the color of the line
  $linecolor=imagecolorallocate ($image, rand (80,220), rand (80,220), rand (80,220));
  //set line, two points and one line
  imageline ($image, rand (1,99), rand (1,29), rand (1,99), rand (1,29), $linecolor);
 }
 //2>Set the head,image/png
 header ("content-type:image/png");
 //3>imagepng () create png graphics function
 imagepng ($image);
 //4>imagedestroy () End graphics function Destroy $image
 imagedestroy ($image);

Next is the code for the static page:index.html

<! Doctype html>
<html>
 <head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8">
  <title>Confirmation Verification Code</title>
 </head>
 <body>
  <form method="post" action="./form.php">
   <P>Verification code:<img border="1" src="./captcha.php?r =<?php echo rand ();
    <a href="javascript:void (0)" onclick="document.getelementbyid (" captcha_img "). Src =" ./captcha.php?r="+ math.random ()">change?</a>
   </p>
   <p>Please enter the verification code:<input type="text" name="authcode" value="" /></p>
   <p><input type="submit" value="submit" /></p>
 </body>
</html>

As you can see from index.html,The submitted form is to form.php, so there is also 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 pages
      echo "<script language=\" javascript \ ">";
      echo "alert (" Input error! ");";
      echo "document.location=\" ./form.php \ "";
      echo "</script>";
    }
    exit ();
  }

The page displayed is as follows:

Well, pure numbers are achieved,It should not be difficult to add numbers to English,stop talking nonsense,Pull the code.

<?php
 //10>Set session, must be at the top of the script
 session_start ();
 $image=imagecreatetruecolor (100, 30);//1>Function to set the size of the captcha image
 //5>Set the captcha color imagecolorallocate (int im, int red, int green, int blue);
 $bgcolor=imagecolorallocate ($image, 255,255,255);//#ffffff
 //6>area fill int imagefill (int im, int x, int y, int col) (x, y) where the area is colored,col indicates the color to be painted
 imagefill ($image, 0, 0, $bgcolor);
 //10>set variables
 $captcha_code="";
 //7>Generate random letters and numbers
 for ($i=0;$i<4;$i ++) {
  //Set the 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 value to be taken randomly,Remove error-prone values ​​such as 0 and o
  $data="abcdefghigkmnpqrstuvwxy3456789";
  //Retrieve the value, the string interception method strlen gets the string length
  $fontcontent=substr ($data, rand (0, strlen ($data)), 1);
  //10>.=Continuously define variables
  $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>increase interference elements,Set snowflake points
 for ($i=0;$i<200;$i ++) {
  //Set the color of the point,50-200 colors are lighter than numbers,Does not interfere with reading
  $pointcolor=imagecolorallocate ($image, rand (50,200), rand (50,200), rand (50,200));
  //imagesetpixel — draw a single pixel
  imagesetpixel ($image, rand (1,99), rand (1,29), $pointcolor);
 }
 //9>Add interference elements,Set horizontal lines
 for ($i=0;$i<4;$i ++) {
  //Set the color of the line
  $linecolor=imagecolorallocate ($image, rand (80,220), rand (80,220), rand (80,220));
  //set line, two points and one line
  imageline ($image, rand (1,99), rand (1,29), rand (1,99), rand (1,29), $linecolor);
 }
 //2>Set the head,image/png
 header ("content-type:image/png");
 //3>imagepng () create png graphics function
 imagepng ($image);
 //4>imagedestroy () End graphics function Destroy $image
 imagedestroy ($image);

The other two pages,No modification is allowed.

Generally speaking,That's enough for now.But just like anime,There will always be foreign.

Well, let's have a Chinese character.In fact, I also plan to put the verification code of Chinese characters into my graduation design.Although swipe codes are very popular now,But I am not specialized in learning JS after all.

<?php
 //11>set session, must be at the top of the script
 session_start ();
 //1>Function to set the captcha picture size
 $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
 //6>area fill int imagefill (int im, int x, int y, int col) (x, y) where the area is colored,col indicates the color to be painted
 imagefill ($image, 0, 0, $bgcolor);
 //7>set ttf font
 $fontface="fzytk.ttf";
 //7>Set the font library,Achieving a simple digital reserve
 $str="Benevolence in heaven and earth regards all things as savage dogs. Sage innocence regards people as ruminant dogs. This phrase often appears in the complaints of tyrants and tyranny. Cruel and innocent treats all things as lowly pigs and dogs, and those so-called saints who are high above are no different It's not that our people are not as good as pigs and dogs, but the real interpretation is that the ground is not emotional and treats all things as equals. The sage is not emotional and treats the people as equals. The hand of the child and the son of the old hero and heroine looked at each other with affection. The hand of Sentence Holder and the old lady's tears dimly and shyly answered a nasty sentence. Have we seen such a plot, but let's take a look at the original sentence of this sentence. Yu Yukuo, I do n’t live, Yu I do n’t believe, I mean, the agreement between the soldiers said that they would die together. Now the people I have agreed to are gone. How can I live?The basic love of Jiyou is much more suitable than the love between men and women. "
 //str_split () splits the string into an array,One Chinese in utf_8 is 3 characters
 $strdb=str_split ($str, 3);
 //>11
 $captcha_code="";
 //8>Generate random man
 for ($i=0;$i<4;$i ++) {
  //Set the font color,random color
  $fontcolor=imagecolorallocate ($image, rand (0,120), rand (0,120), rand (0,120));//0-120 dark color
  //randomly select Chinese
  $in=rand (0, count ($strdb));
  $cn=$strdb [$in];
  //Record Chinese into a 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) curtain, size, angle, coordinates, color, font path,Text string
  mt_rand () generates better random numbers,Four 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 elements,Set point
 for ($i=0;$i<200;$i ++) {
  //Set the color of the point,50-200 colors are lighter than numbers,Does not interfere with reading
  $pointcolor=imagecolorallocate ($image, rand (50,200), rand (50,200), rand (50,200));
  //imagesetpixel — draw a single pixel
  imagesetpixel ($image, rand (1,199), rand (1,59), $pointcolor);
 }
 //10>increase interference elements,Set line
 for ($i=0;$i<4;$i ++) {
  //Set the color of the line
  $linecolor=imagecolorallocate ($image, rand (80,220), rand (80,220), rand (80,220));
  //set line, two points and one line
  imageline ($image, rand (1,199), rand (1,59), rand (1,199), rand (1,59), $linecolor);
 }
 //2>Set the head,image/png
 header ("content-type:image/png");
 //3>imagepng () create png graphics function
 imagepng ($image);
 //4>imagedestroy () End graphics function Destroy $image
 imagedestroy ($image);

No other pages need to be modified.

php
  • Previous C # inline-asm solve embedded x86 assembly
  • Next winform simple cache class instance