PHP generates various verification codes and AJAX validation using jquery

Source: Internet
Author: User

PHP generated Verification code image PHP code to generate the principle: the use of PHP's GD library, generate a picture with a verification code, and save the Verification code in the session. PHP generated verification code of the approximate process is:1, produce a PNG picture,2, set the background color for the picture,3, set the font color and style; 4 and generates a random verification code of 4 digits; 5 , adjust the rotation angle and position of each character produced to the PNG image; 6 , adding noise and interference lines to prevent the registration of machine analysis of the original image to maliciously crack verification code; 7 , output pictures; 8
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)//The generated verification code is written to the SESSION, the verification with $_session["verify_code"] = $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 < 80;     $i + +) {Imagesetpixel ($im, rand (0, $w), rand (0, $h), $black);     The numbers are randomly displayed on the canvas, and the horizontal spacing and position of the characters are randomly generated according to a certain range of fluctuations $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 (): Assign color to Image Imagefill (): Fill Image Imagerectangle (): Draw a rectangle (border) Imagesetstyle () : Set line drawing Style Imageline (): Draw a Segment Imagesetpixel (): Draw a point pixel imagepng (): Output the image to a browser or file in PNG format Imagedestroy (): Free up the memory of the image save the above code as Code_ num.php so that it can be called.
After the Ajax refresh and verification code generation, we want to apply in the actual project, usually we use AJAX can implement 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:
<p> captcha: <input type= "text" class= "input" id= "Code_num" name= "Code_num" maxlength= "4"/>   </p>  
in the HTML code,<img src="code_num.php" called the generated verification code, and when the verification code is clicked, the refresh generates a new CAPTCHA:
$ (function () {     //digital 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 ("Captcha correct!") ");             } else{                 Alert ("CAPTCHA Error! ");             }         });     });  
Background chk_code.php Verification:
Session_Start ();   $code = Trim ($_post[' code ');  if ($code ==$_session["Verify_code"]) {   
The background is verified by the verification code that is submitted and saved in the session.


PHP generates various verification codes and AJAX validation using jquery

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.