This article mainly introduces php verification code implementation, pure digital verification code, numbers plus English verification code, and a Chinese character Verification Code. For more information, see
This article mainly introduces php verification code implementation, pure digital verification code, numbers plus English verification code, and a Chinese character Verification Code. For more information, see
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 graphic function imagepng ($ image); // 4> imagedestroy () End the graphic function to destroy $ image imagedestroy ($ image );
The code for the static page is: index.html
Confirm Verification Code
Verification Code: '/> for another one?
Enter the verification code:
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"