Verification code in the form to achieve more and more, but with JS write the verification code, always feel inconvenient, so learn the next PHP implementation of the verification code.
Well, there is nothing to do, but do not want to waste time, so learn the next PHP implementation verification code. Is the so-called, more than the pressure of the body. Also, it can be encapsulated into a function, it is convenient to use later, of course, now not encapsulated.
Now let's talk about simple, pure digital verification code.
If it's a beginner, it's recommended to follow my code's comments/numbers step by step. The easiest 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> a 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 The//6> area fills the area of the int imagefill (int im, int x, int y, int col) (x,y), col represents the color to be painted Imagefill ($image, 0, 0, $b
Gcolor);
10> Set Variable $captcha _code = "";
7> generates a random number for ($i =0; $i <4; $i + +) {//set font size $fontsize = 6; Set font color, random color $fontcolor = imagecolorallocate ($image, Rand (0,120), Rand (0,120), Rand (0,120));
0-120 Deep Color//Set Number $fontcontent = rand (0,9);
10>.= continuously defines variables $captcha _code. = $fontcontent;
Set the 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 elements, set snowflake point for ($i =0; $i <200; $i + +) {//Set the color of the point, 50-200 color is lighter than the number, do not interfere with 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 elements, set horizontal for ($i =0 $i <4; $i + +) {//set line Color $linecolor = Imagecolorallocate ($image, Rand (80,220), Rand (80,2
), Rand (80,220));
Set line, 2.1-line 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 () set up PNG graphics function imagepng ($image); 4>imagedestroy () ends the graphics function to destroy $image Imagedestroy ($image);
Then there is the code for the static page: index.html
As you can see from the index.html, the submitted form is to form.php, so there's a form.php code to judge:
<?php
Header ("Content-type:text/html;charset=utf-8"); Set Header information
//isset () detect if the variable is set to
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 as well as jump page
echo "<script language=\" javascript\ ">";
echo "Alert (' Input error! ');";
echo "document.location=\"./form.php\ "";
echo "</script>";
}
Exit ();
}
The display page is as follows:
Then, the realization of pure numbers, the number plus English should not be difficult, nonsense not to say, pull the code bar.
<?php//10> Set session, must be at the top of the script session_start (); $image = Imagecreatetruecolor (100, 30);
1> a 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 The//6> area fills the area of the int imagefill (int im, int x, int y, int col) (x,y), 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 font color, random color $fontcolor = imagecolorallocate ($image, Rand (0,120), Rand (0,120), Rand (0,120));
0-120 Deep Color/Set the value to be randomly taken, remove error-prone values such as 0 and o $data = ' abcdefghigkmnpqrstuvwxy3456789 ';
Fetch value, String intercept method strlen gets string length $fontcontent = substr ($data, Rand (0,strlen ($DATA)), 1);
10>.= continuously defines variables $captcha _code. = $fontcontent;
Set the 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> IncreaseAdd interference elements, set snowflake point for ($i =0; $i <200; $i + +) {//Set point color, 50-200 color lighter than the number, do not interfere with 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 elements, set horizontal for ($i =0 $i <4; $i + +) {//set line Color $linecolor = Imagecolorallocate ($image, Rand (80,220), Rand (80,2
), Rand (80,220));
Set line, 2.1-line 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 () set up PNG graphics function imagepng ($image); 4>imagedestroy () ends the graphics function to destroy $image Imagedestroy ($image);
The other two pages are not allowed to be modified.
Generally speaking, it is enough now. But like anime, there's always a way out.
So, let's have a Chinese character. In fact, I am also ready to test the Chinese characters to my graduation design inside, although now very popular slide verification code, but I am not specifically learning JS.
<?php//11> Set session, must be at the top of the script session_start ();
1> a function $image = Imagecreatetruecolor (200, 60) to set the size of the validation code picture;
5> Set the authentication code color imagecolorallocate (int im, int red, int green, int blue); $bgcolor = Imagecolorallocate ($image, 255,255,255); #ffffff The//6> area fills the area of the int imagefill (int im, int x, int y, int col) (x,y), col represents the color to be painted Imagefill ($image, 0, 0, $b
Gcolor); 7> Set TTF Font $fontface = ' fzytk.
TTF '; 7> set the font, realize a simple digital reserve $str = ' Heaven and earth are unkind to all things and the dog Saints are unkind to the common people as a humble dog this often appears in the accusation of tyranny and brutality that treats everything as a lowly pig dog and those above the so-called holy people are also not the same as our common people also as pigs and dogs as inferior to things but really is to take the interpretation is not emotional personally to all things equal to the Saints not emotional personally to the people Equal to the hand and son of the sons and women when the hero lovingly looked at each other said a sentence to hold the son of the hand and the son of the old woman tearful dim blushing back to a hate this episode we have not seen a lot but let's take a look at this sentence of the original sentence of death and Ze Cheng said that the hands and son to hold the son grow old in sigh broad XI no I live XI Yu Ying XI No I believe it means to say the agreement between the Warriors
Said to die together now and I agreed to the people are gone how do I live ah naked Brothers and lakes comrades friendship to describe a good base of friends with love than men and women between the right to a lot of it ';
Str_split () Cutting string is 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 font color, random color $fontcolor = imagecolorallocate ($image, Rand (0,120), rand (0,120) , Rand (0,120)); 0-120 Deep Color//random selection of Chinese $in = rand (0,count ($STRDB));
$CN = $strdb [$in];
Records Chinese to 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) Curtains, dimensions, angles, coordinates, colors, font paths, text strings Mt_rand () generate better random numbers, faster than rand () four times times * * 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 color is lighter than the number, do not interfere with reading $pointcolor = Imagecolorallocate ($image,
Rand (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,2
), Rand (80,220));
Set line, 2.1-line 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 () set up PNG graphics function imagepng ($image); 4>imagedestroy () ends the graphics function to destroy $image Imagedestroy ($image);
Other pages do not need to be modified.
The effect chart is as follows:
The above is the PHP implementation of the three kinds of verification code: pure Digital Verification Code, the number of English verification code, there is a Chinese character verification code, I hope to be proficient in PHP verification code to help.