PHP generates a variety of common verification code and AJAX verification process, PHP Verification code AJAX Verification
Verification codes are important in Web applications and are often used to prevent users from maliciously submitting forms, such as malicious registration and login, malicious forum flooding, and so on. This article will explain the use of PHP to generate a variety of common verification code including digital verification Code, Digital + letter verification Code, Chinese verification Code, arithmetic verification code, and its AJAX verification process.
PHP Generate Verification Code picture
PHP generated Verification Code principle: the use of PHP's GD library, generate a picture with a verification code, and save the Verification code in the session.
The approximate process for PHP to generate verification codes is:
1. Create a picture of PNG;
2. Set the background color for the picture;
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 create a PNG image;
6. Add noise and interference lines to prevent the registration of machine analysis of the original image to maliciously crack verification code;
7. Output picture;
8. Release the memory occupied by the image.
Example:
Session_Start (); GetCode (4,60,20); function GetCode ($num, $w, $h) {$code = ""; for ($i = 0; $i < $num; $i + +) {$code. = rand (0, 9);}//4-bit verification code can also be generated directly with Rand (1000,9999)//To write the generated verification code to the session, for verification with $_sessio n["Helloweba_num"] = $code; Create a picture, 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 Background imagefill ($im, 0, 0, $gray); Draw Border Imagerectangle ($im, 0, 0, $w-1, $h-1, $black); Randomly draws two dashed lines, acts as a disturbance $style = Array ($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray); Imagesetstyle ($im, $style); $y 1 = rand (0, $h); $y 2 = rand (0, $h); $y 3 = rand (0, $h); $y 4 = rand (0, $h); Imageline ($im, 0, $y 1, $w, $y 3, img_color_styled); Imageline ($im, 0, $y 2, $w, $y 4, img_color_styled); A large number of black dots are randomly generated on the canvas, which acts as interference; for ($i = 0; $i <, $i + +) {Imagesetpixel ($im, rand (0, $w), rand (0, $h), $black), and//numbers are randomly displayed on the canvas, and the horizontal spacing and position of the characters are in accordance with the range of fluctuations Machine generated $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 Picture Imagedestroy ($im);//release Image Memory}
In the code, the Custom Function GetCode () interprets the validation code generation process. Using the PHP GD Library's own image processing function, can easily generate a variety of desired image effects.
Imagecreate (): Create a new image
Imagecolorallocate (): Assigning colors to images
Imagefill (): Fill image
Imagerectangle (): Draw a rectangle (border)
Imagesetstyle (): Set the line drawing style
Imageline (): Draw a line segment
Imagesetpixel (): Draw dots of pixels
Imagepng (): Output images to a browser or file in PNG format
Imagedestroy (): Release the memory of the image
Save the above code as code_num.php so that it can be called.
Ajax Refresh and Validation
After the verification code is generated, we are going to apply it in the actual project, usually we can use AJAX to achieve the Click Verification Code refresh generate a new verification code (sometimes generated verification code is difficult to recognize the naked eye), that is, "can't see a change." After completing the verification code, you also need to verify that the verification code is correct, the process of verification is to the background program to complete, but we can also use Ajax to achieve no refresh verification.
We set up a front-end page index.html, load jquery, and add a captcha form element to the body:
Verification Code:
In the HTML code,
$ (function () {//Numeric authentication $ ("#getcode_num"). Click (function () {$ (this). attr ("src", ' code_num.php? ' + math.random ());}); });
Refresh the verification code, in fact, is to re-request the verification code generator, it is important to note that when calling code_num.php with random parameters to prevent caching. Next fill in the Verification code, click the "Submit" button, through $.post (), the front-end back to the chk_code.php to send AJAX requests.
$ (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 CAPTCHA is correct! "); } else{Alert ("CAPTCHA Error! "); } }); }); });
Background chk_code.php Verification:
Session_Start (); $code = Trim ($_post[' code '); if ($code ==$_session["Helloweba_num"]) {echo ' 1 ';}
The background is verified by the verification code that is submitted and saved in the session.
For the other several kinds of verification of the generation and use, the same principle, the developer can produce a variety of styles of random verification Code, the demo provides a digital verification code, Digital + letter verification Code, Chinese verification Code, imitation Google Verification Code, arithmetic verification code and so on. Limited to space, the other types of verification code generated code skipped, please understand.
Articles you may be interested in:
- Improvement of Php+jquery Registration Module (i): Verification code stored in session
- PHP code to make graphics verification codes share
- thinkphp implementation of File upload function with verification code example
- A classic PHP Verification code class sharing
- PHP program always prompts for incorrect verification code input solution
- PHP implementation of IMEI limit SMS verification code send class
http://www.bkjia.com/PHPjc/1091113.html www.bkjia.com true http://www.bkjia.com/PHPjc/1091113.html techarticle PHP generates a variety of common verification code and AJAX verification process, PHP Verification code AJAX Verification Code is very important in web applications, usually to prevent users from maliciously submitting forms, such as malicious registration and login ...