PHP verification code-Ajax verification implementation method-PHP Tutorial

Source: Internet
Author: User
Tags set background
PHP verification code-Ajax verification implementation method. In website development, most of us use ajax to perform some operations to provide user experience, next, I will introduce a friend who needs to use ajax to implement the verification code for refreshing pages. in order to provide user experience, most of them use ajax for some operations in website development, next, I will introduce a friend who needs to use ajax to implement the verification code ajax verification for the refreshing page.

I will not introduce the verification code generation program here, you can refer to the http://www.bKjia. c0m/phper/phpanqn/46698.htm below to introduce a simple

The code is as follows:

Session_start ();
// Settings: you can modify the parameters of the verification code image here.
$ Image_width = 120;
$ Image_height = 40;
$ Characters_on_image = 6;
$ Font = './monofont. ttf ';

// The following characters will be used in the verification code
// Remove the number 1 and letter I to avoid confusion
$ Possible_letters = '23456789bcdfghjkmnpqrstvwxyz ';
$ Random_dots = 10;
$ Random_lines = 30;
$ Captcha_tex_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']);

/* Randomly generate interference noise on the background */
For ($ I = 0; $ I <$ random_dots; $ I ++ ){
Imagefilledellipse ($ image, mt_rand (0, $ image_width ),
Mt_rand (0, $ image_height), 2, 3, $ image_noise_color );
}

/* Randomly generate lines on the background image */
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 sketch 6 characters in it */
$ 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 image on the HTML page */
Header ('content-Type: image/jpeg ');
// Set the image output type
Imagejpeg ($ image );
// Display the image
Imagedestroy ($ image );
// Destroy an image instance
$ _ 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 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, "cannot see or change 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.

The verification code is correct or incorrect.
The text on the verification code image is stored in the SESSION variable. during verification, we need to compare the value in the SESSION with the value entered by the user.

$ _ SESSION [6_letters_code]-text value containing the verification code
$ _ POST [6_letters_code]-the content of the verification code entered by the user


We have created a front-end page index.html, loaded jquery, and added the verification code form element to the body:

The code is as follows:

Verification code:


In html code,

The code is as follows:

$ (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.

The code is as follows:

$ (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:

The code is as follows:

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.

...

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.