The implementation method of PHP verification code AJAX Verification

Source: Internet
Author: User
Tags html page set background

Verification Code Generation Program I do not introduce here, you can refer to http://www.111cn.net/phper/phpanqn/46698.htm below to introduce a simple

The code is as follows Copy Code

<?php
Session_Start ();
Settings: You can modify the parameters of the verification code picture here
$image _width = 120;
$image _height = 40;
$characters _on_image = 6;
$font = './monofont.ttf ';

The following characters will be used to verify the characters in the code
To avoid confusion, the number 1 and the letter I are removed.
$possible _letters = ' 23456789bcdfghjkmnpqrstvwxyz ';
$random _dots = 10;
$random _lines = 30;
$captcha _text_color= "0x142864";
$captcha _noice_color = "0x142864";

$code = ';

$i = 0;
while ($i < $characters _on_image) {
$code. = substr ($possible _letters, Mt_rand (0, strlen ($possible _letters)-1), 1);
$i + +;
}

$font _size = $image _height * 0.75;
$image = @imagecreate ($image _width, $image _height);

/* Set background, text, and interference noise.
$background _color = imagecolorallocate ($image, 255, 255, 255);

$arr _text_color = Hexrgb ($captcha _text_color);
$text _color = imagecolorallocate ($image, $arr _text_color[' red '),
$arr _text_color[' green '], $arr _text_color[' Blue ');

$arr _noice_color = Hexrgb ($captcha _noice_color);
$image _noise_color = imagecolorallocate ($image, $arr _noice_color[' red '),
$arr _noice_color[' green '], $arr _noice_color[' Blue ');

* * On the background of the random generation of interference noise.
for ($i =0; $i < $random _dots; $i + +) {
Imagefilledellipse ($image, Mt_rand (0, $image _width),
Mt_rand (0, $image _height), 2, 3, $image _noise_color);
}

* * In the background picture, randomly generated lines * *
for ($i =0; $i < $random _lines; $i + +) {
Imageline ($image, Mt_rand (0, $image _width), Mt_rand (0, $image _height),
Mt_rand (0, $image _width), Mt_rand (0, $image _height), $image _noise_color);
}

/* Generate a text box, and then sketch the inside 6 characters * *
$textbox = Imagettfbbox ($font _size, 0, $font, $code);
$x = ($image _width-$textbox [4])/2;
$y = ($image _height-$textbox [5])/2;
Imagettftext ($image, $font _size, 0, $x, $y, $text _color, $font, $code);

/* Display the verification code picture on the HTML page * *
Header (' Content-type:image/jpeg ');
Set the type of picture output
Imagejpeg ($image);
Show pictures
Imagedestroy ($image);
Destroying picture instances
$_session[' 6_letters_code ' = $code;

function Hexrgb ($HEXSTR) {
$int = Hexdec ($HEXSTR);

Return Array ("Red" => 0xFF & ($int >> 0x10),
"Green" => 0xFF & ($int >> 0x8),
"Blue" => 0xFF & $int
);
}
?>

Verification Code

After the build, we have to apply in the actual project, usually we use AJAX to achieve the click of 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.

Ways to verify that the validation code is correct or wrong
The text on the verification code picture is stored in the session variable, and when it is validated, we need to compare the values of the sessions with the values entered by the user.

$_session[6_letters_code]– the literal value of the verification code stored
$_post[6_letters_code]– This is the content of the authentication code entered by the user


We build a front page index.html, load jquery, and add a captcha form element to the body:

The code is as follows Copy Code

<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

The code is as follows Copy Code

$ (function () {
Digital verification
$ ("#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.

  code is as follows copy code

$ (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!) ");  
            } 
   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP});  
    });  
};  


Background chk_code.php Validation:

The code is as follows Copy Code

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.

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.