Verification code is very important in Web application, which is usually used to prevent users from submitting forms maliciously, such as malicious registration and login, forum malicious irrigation, etc. This article will illustrate 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, etc. and its AJAX verification process.
PHP Generate CAPTCHA Picture
PHP to generate the principle of verification code: the use of PHP's GD library, to generate a picture with the verification code, and the verification code stored in the session.
The approximate process for PHP to generate verification code is:
1. Create a photo 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 produced to the PNG picture;
6. Add noise and interference lines to prevent the registration machine analysis of the original image to malicious crack authentication code;
7. Output picture;
8. Free up the memory of the picture.
Example:
Session_Start ();
GetCode (4,60,20);
function GetCode ($num, $w, $h) {$code = "";
for ($i = 0; $i < $num; $i + +) {$code. = rand (0, 9);
The///4-bit captcha can also be generated directly by rand (1000,9999)//To write the generated validation code into session, with $_session["helloweba_num" = $code;
Create a picture that defines 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 a border Imagerectangle ($im, 0, 0, $w-1, $h-1, $black);
Randomly draw two dotted lines, interfering $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 spots are randomly generated on the canvas, which plays a disturbing role.
for ($i = 0; $i < $i + +) {Imagesetpixel ($im, rand (0, $w), rand (0, $h), $black); ///Display the numbers randomly in the pictureOn the cloth, the horizontal spacing and position of the characters are randomly generated according to 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 Picture Imagedestroy ($im);//Free up memory for pictures}
In the code, the Custom Function GetCode () interprets the validation code generation process. PHP Library with the use of the image processing functions, can easily generate a variety of desired picture effects.
Imagecreate (): Create a new image
Imagecolorallocate (): Assigning colors to images
Imagefill (): Populating images
Imagerectangle (): Draw a rectangle (border)
Imagesetstyle (): Set the style of drawing lines
Imageline (): Draw a line segment
Imagesetpixel (): Dot Pixel
Imagepng (): Output the image to a browser or file in PNG format
Imagedestroy (): Free up memory of the picture
Save the above code as code_num.php so that it can be invoked.
Ajax Refresh and Validation
After the verification code generation, we want to apply in the actual project, we usually use AJAX to achieve click on the verification code to refresh the generation of new verification code (sometimes generated verification code is difficult to recognize the naked eye), that is, "see a change." After completing the verification code, you also need to verify that the completed verification code is correct, the verification process is to the background program to complete, but we can also use Ajax to achieve no refresh verification.
We build a front page index.html, load jquery, and add a captcha form element to the body:
<p> Verification Code: <input type= "text" class= "input" id= "Code_num" name= "Code_num" maxlength= "4"/> </p>
<p><input type=" Button "class=" btn "id=" Chk_num "value=" Submit "/></p>
In the HTML code, the
$ (function () {
//digital authentication
$ ("#getcode_num"). Click (function ()
{$ (this). attr ("src", ' code_num.php? ') Math.random ());
...
});
Refreshing the verification code, in fact, is to request a code generation program, here to note that the call code_num.php to take random parameters to prevent caching. Next fill in the Verification code, click the "Submit" button, through $.post (), front-end back to the chk_code.php send Ajax request.
$ (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 ("Verify code is correct! ");
} else{
alert ("Authentication code Error!) ");
}
});
});
});
Background chk_code.php Validation:
Session_Start ();
$code = Trim ($_post[' code ']);
if ($code ==$_session["Helloweba_num"]) {
echo ' 1 ';
}
In the background, the verification code is compared with the verification code saved in session.
For other kinds of validation of the generation and use of the same principle, developers can be based on the need to produce a variety of styles of random authentication code, this demonstration demo provides a digital verification code, Digital + letter verification Code, Chinese verification Code, imitation Google Verification Code, arithmetic verification code. Limited to space, the other kinds of verification code generation code skipped, please understand.