Php Verification Code (3 types) and php Verification Code (3 types)

Source: Internet
Author: User
Tags add numbers

Php Verification Code (3 types) and php Verification Code (3 types)

More and more verification codes are implemented in forms, but it is inconvenient to use the verification codes written in js. Therefore, I learned the verification codes implemented by php.

Well, there is nothing to do, but I don't want to waste time, so I learned how to implement the verification code in php. This is what we call it. In addition, it can also be encapsulated into a function, which will be convenient to use in the future. Of course, it is not encapsulated now.
Now let's talk about a simple pure digital verification code.
If you are a beginner, we recommend that you follow the Notes/numbers in my code step by step. The simplest method is to copy the entire code.
Create a new captcha. php:

<? Php // 10> set the session, which must be at the top of the script session_start (); $ image = imagecreatetruecolor (100, 30 ); // 1> function for setting the verification code image size // 5> set the verification code color imagecolorallocate (int im, int red, int green, int blue); $ bgcolor = imagecolorallocate ($ image, 255,255,255); // # ffffff // 6> Fill in the area where int imagefill (int im, int x, int y, int col) (x, y) is located, col indicates the color of imagefill ($ image, 0, 0, $ bgcolor) to be coated. // 10> set the variable $ 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 deep color // set the number $ fontcontent = rand (); // 10>. = continuous definition variable $ captcha_code. = $ fontcontent; // Set coordinates $ x = ($ I * 100/4) + rand (); $ y = rand (); imagestring ($ image, $ fontsize, $ x, $ y, $ fontcontent, $ fontcolor);} // 10> Save to session $ _ SESSION ['authcode'] = $ captcha_code; // 8> Add interference elements, set snowflake points for ($ I = 0; $ I <200; $ I ++) {// set the color of the point. The color of 50-is lighter than the number, read $ pointcolor = imagecolorallocate ($ image, rand (50,200), rand (50,200), rand (50,200); // imagesetpixel-draw a single pixel imagesetpixel ($ image, rand (), rand (), $ pointcolor);} // 9> Add interference elements and set the horizontal line for ($ I = 0; $ I <4; $ I ++) {// set the line color $ linecolor = imagecolorallocate ($ image, rand (80,220), rand (80,220), rand (80,220 )); // set the line, 2.1 line imageline ($ image, rand (), $ linecolor );} // 2> set the header, image/png header ('content-Type: image/png '); // 3> imagepng () create a png image function imagepng ($ image ); // 4> imagedestroy () end graph function destroy $ image imagedestroy ($ image );

The code for the static page is: index.html

<! Doctype html> 

As you can see from index.html, the submitted form is in form. php, so there is also a judgment form. php code:

<? Php header ("Content-Type: text/html; charset = UTF-8"); // set the header information // isset () check if the variable is set if (isset ($ _ REQUEST ['authcode']) {session_start (); // strtolower () lower case function if (strtolower ($ _ REQUEST ['authcode']) === _ SESSION ['authcode']) {// jump to the page echo "<script language = \" javascript \ ">"; echo "document. location = \". /form. php \ ""; echo "</script>";} else {// prompt and jump page echo "<script language = \" javascript \ "> "; echo "alert ('input error! '); "; Echo" document. location = \ "./form. php \" "; echo" </script> ";}exit ();}

The displayed page is as follows:

Then, the implementation of pure numbers should not be difficult to add numbers to English. If you don't need to talk about it, pull the code.

<? Php // 10> set the session, which must be at the top of the script session_start (); $ image = imagecreatetruecolor (100, 30 ); // 1> function for setting the verification code image size // 5> set the verification code color imagecolorallocate (int im, int red, int green, int blue); $ bgcolor = imagecolorallocate ($ image, 255,255,255); // # ffffff // 6> Fill in the area where int imagefill (int im, int x, int y, int col) (x, y) is located, col indicates the color of imagefill ($ image, 0, 0, $ bgcolor) to be coated. // 10> set the variable $ 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 deep color // set the value to be random and remove the error-prone values such as 0 and o $ data = 'abcdefghigkmnpqrstuvwxy3456789 '; // retrieve the value, string truncation method strlen get String Length $ fontcontent = substr ($ data, rand (0, strlen ($ data), 1); // 10>. = continuous definition variable $ captcha_code. = $ fontcontent; // Set coordinates $ x = ($ I * 100/4) + rand (); $ y = rand (); imagestring ($ image, $ fontsize, $ x, $ y, $ fontcontent, $ fontcolor);} // 10> Save to session $ _ SESSION ['authcode'] = $ captcha_code; // 8> Add interference elements, set snowflake points for ($ I = 0; $ I <200; $ I ++) {// set the color of the point. The color of 50-is lighter than the number, read $ pointcolor = imagecolorallocate ($ image, rand (50,200), rand (50,200), rand (50,200); // imagesetpixel-draw a single pixel imagesetpixel ($ image, rand (), rand (), $ pointcolor);} // 9> Add interference elements and set the horizontal line for ($ I = 0; $ I <4; $ I ++) {// set the line color $ linecolor = imagecolorallocate ($ image, rand (80,220), rand (80,220), rand (80,220 )); // set the line, 2.1 line imageline ($ image, rand (), $ linecolor );} // 2> set the header, image/png header ('content-Type: image/png '); // 3> imagepng () create a png image function imagepng ($ image ); // 4> imagedestroy () end graph function destroy $ image imagedestroy ($ image );

The other two pages cannot be modified.

In general, it is enough now. But like an anime, there will always be a fan of it.
Let's get a Chinese character out of the box. In fact, I am also preparing to put the Chinese character verification code into my graduation project. Although sliding verification code is very popular now, I am not dedicated to js after all.

<? Php // 11> set the session, which must be at the top of the script session_start (); // 1> the function of setting the image size of the Verification Code $ image = imagecreatetruecolor (200, 60 ); // 5> set the verification code color imagecolorallocate (int im, int red, int green, int blue); $ bgcolor = imagecolorallocate ($ image, 255,255,255 ); // # ffffff // 6> Fill in the area where int imagefill (int im, int x, int y, int col) (x, y) is located, col indicates the color of imagefill ($ image, 0, 0, $ bgcolor) to be coated. // 7> set ttf font $ fontface = 'fzytk. ttf'; // 7> set the font, implement a simple digital reserve $ str = 'Heaven and Earth are insensitive to all things, take all things as the neighborhood, and take the people as the neighborhood. This sentence is often seen in the manipulation of the tyrant and the neighborhood of everything the so-called holy people, who are high in height, do not treat our common people as things that are not as good as pigs and dogs. But the real positive interpretation is that they are not emotional and treat all things equally, saints are not emotional. the people treat each other with a hand, and the old man look at the other party and say a hand, and the old man, the old man, with tears, and a sigh of relief. Have we ever seen a lot of such a plot? however, let's take a look at the original sentence of this sentence. The original sentence is just a piece of cake, and the child is always a piece of cake. If I don't live, I don't believe it. It means the Agreement between soldiers. I want to die together. Now I agree that people are gone. How can I live? Naked Brothers, rivers and lakes, and friendship? It's better to describe the foundation of friends than love between men and women'; // The str_split () Cut string is an array, and a Chinese character in utf_8 is 3 characters $ strdb = str_split ($ str, 3); //> 11 $ captcha_code = ''; // 8> Generate a 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 deep color // randomly select Chinese $ in = rand (0, count ($ strdb); $ cn = $ strdb [$ in]; // record Chinese characters to the string saved to the session $ captcha_code. = $ cn;/* imagettftext (resource $ image, float $ size, float $ angle, int $ x, int $ y, int $ color, string $ fontfile, string $ text) canvas, size, angle, coordinate, color, font path, text string mt_rand () to generate a better random number, 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 and set the point to for ($ I = 0; $ I <200; $ I ++) {// you can specify the color of a vertex. The color ranges from 50 to 50,200 to a shorter value than that of a digit. For more information, see $ pointcolor = imagecolorallocate ($ image, rand (50,200), rand ), rand (50,200); // imagesetpixel-draw a single pixel imagesetpixel ($ image, rand (1,199), rand (), $ pointcolor );} // 10> Add interference elements and set the line for ($ I = 0; $ I <4; $ I ++) {// set the line color $ linecolor = imagecolorallocate ($ image, rand (80,220), rand (80,220), rand (80,220); // set the line, 2.1 line imageline ($ image, rand (1,199), rand (1,199), rand (), rand (), $ linecolor);} // 2> set the header, image/png header ('content-Type: image/png '); // 3> imagepng () create a png image function imagepng ($ image); // 4> imagedestroy () destroy the graphic function $ image imagedestroy ($ image );

Other pages do not need to be modified.

As follows:

The above are three verification codes implemented by php: pure digital verification codes, numbers plus English verification codes, and a Chinese character verification code. I hope it will help you master php verification codes.

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.