This document describes how to use PHP to generate various common verification codes, including digital verification codes, numbers + letters verification codes, Chinese verification codes, and arithmetic verification codes. the Ajax verification process is very important in WEB applications, it is usually used to prevent users from submitting forms maliciously, such as malicious registration and login, and malicious Forum bumping. This article describes how to use PHP to generate various common verification codes, including the digit verification code, digit + letter verification code, Chinese verification code, arithmetic verification code, and its Ajax verification process.
PHP generates verification code Image
How PHP generates a verification code: Use the GD library of PHP to generate an image with the verification code and save the verification code in the Session.
The general process for generating a verification code in PHP is as follows:
1. generate a png image;
2. set the background color for the image;
3. set the font color and style;
4. generate a 4-digit random verification code;
5. adjust the rotation angle and position of each character to png images;
6. add noise and interference lines to prevent the registered machine from maliciously cracking the verification code by analyzing the original image;
7. output the image;
8. release the memory occupied by images.
Example:
Session_start (); getCode (4,60, 20); function getCode ($ num, $ w, $ h) {$ code = ""; for ($ I = 0; $ I <$ num; $ I ++) {$ code. = rand (0, 9);} // A four-digit verification code can also be directly generated using rand (,) // The generated verification code is written into the session, during backup verification, use $ _ SESSION ["helloweba_num"] = $ code; // create an image and define the color value header ("Content-type: image/PNG "); $ im = imagecreate ($ w, $ h); $ black = imagecolorallocate ($ im, 0, 0, 0); $ gray = imagecolorallocate ($ im, 200,200,200 ); $ bgcolor = imagecolorallocate ($ im, 255,255,255); // fill in the background imagefill ($ im, 0, 0, $ gray); // draw the border imagerectangle ($ im, 0, 0, $ W-1, $ H-1, $ black); // randomly draw two dotted lines to interfere with $ style = array ($ black, $ black, $ black, $ gray, $ gray); imagesetstyle ($ im, $ style); $ y1 = rand (0, $ h ); $ y2 = rand (0, $ h); $ y3 = rand (0, $ h); $ y4 = rand (0, $ h); imageline ($ im, 0, $ y1, $ w, $ y3, IMG_COLOR_STYLED); imageline ($ im, 0, $ y2, $ w, $ y4, IMG_COLOR_STYLED ); // randomly generate a large number of black spots on the canvas, which can interfere. for ($ I = 0; $ I <80; $ I ++) {imagesetpixel ($ im, rand (0, $ w), rand (0, $ h), $ black);} // randomly display numbers on the canvas, the horizontal spacing and position of characters are randomly generated according to a certain fluctuation range $ strx = rand (3, 8); for ($ I = 0; $ I <$ num; $ I ++) {$ strpos = rand (1, 6); imagestring ($ im, 5, $ strx, $ strpos, substr ($ code, $ I, 1 ), $ black); $ strx + = rand (8, 12);} imagepng ($ im); // output image imagedestroy ($ im); // release the memory occupied by the image}
In the code, the custom function getCode () interprets the verification code generation process. Using the image processing function provided by the php gd library, you can easily generate various desired image effects.
Imagecreate (): creates a new image.
Imagecolorallocate (): assign color to the image
Imagefill (): fill the image
Imagerectangle (): draw a rectangle (border)
Imagesetstyle (): sets the draw line style.
Imageline (): draw a line segment
Imagesetpixel (): Pixel
Imagepng (): outputs an image to a browser or file in PNG format.
Imagedestroy (): releases the memory occupied by images.
Save the above code as code_num.php for calling.
Ajax refresh and verification
After the verification code is generated, we need to apply it in the actual project. generally, we can use ajax to refresh the new verification code when clicking the verification code (sometimes the generated verification code is hard to recognize by the naked eye ), that is, "you cannot see another one ". After entering the verification code, you also need to verify that the entered verification code is correct. the verification process is completed by the background program, but we can also use ajax to implement no-refreshing verification.
We have created a front-end page index.html, loaded jquery, and added the verification code form element to the body:
Verification code:
In html code,
$ (Function () {// digit verification $ ("# getcode_num "). click (function () {$ (this ). attr ("src", 'Code _ num. php? '+ Math. random ());});...});
To refresh the verification code, you must re-request the verification code generation program. Note that random parameters must be included when you call code_num.php to prevent caching. After entering the verification code, click "submit" and use $. post () to send an ajax request to the backend chk_code.php.
$ (Function (){... $ ("# chk_num "). click (function () {var code_num = $ ("# code_num "). val (); $. post ("chk_code.php? Act = num ", {code: code_num}, function (msg) {if (msg = 1) {alert (" The verification code is correct! ");} Else {alert (" verification code error! ");}});});});
Backend chk_code.php verification:
session_start(); $code = trim($_POST['code']); if($code==$_SESSION["helloweba_num"]){ echo '1'; }
The background compares the submitted verification code with the verification code saved in the session to complete the verification.
For the generation and use of other types of verification, the principle is the same. developers can generate multiple types of random verification codes as needed, this demo provides a number verification code, a number + letter verification code, a Chinese verification code, a google-like verification code, and an arithmetic verification code. The code generated for several other verification codes is skipped. thank you for your understanding.