Ajax Image Verification Code: PHP generated various verification code and AJAX validation

Source: Internet
Author: User
Tags rand

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 picture of PNG;
2, set the background color for the picture;
3, set the font color and style;
4, the generation of 4-digit random verification code;
5, the resulting each character adjusted rotation angle and position to the PNG picture;
6, add noise and interference line to prevent the registration machine analysis of the original picture to malicious crack authentication code;
7, the output picture;
8, free up the image of memory.
In response to a classmate's request, the following we use the Php100.com article comments for example, the verification code to explain the generation process, directly on the code.

Session_Start ();
GetCode (4,60,20);

function GetCode ($num, $w, $h) {
$code = "";
for ($i = 0; $i < $num; $i + +) {
$code. = rand (0, 9);
}
4-bit Authenticode can also be generated directly with Rand (1000,9999)
Writes the generated validation code to session, which is used when validation is
$_session["Helloweba_num"] = $code;
Create a picture, define a 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 dashed lines to play a disturbing role
$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); This article links http://www.cxybl.com/html/wlbc/Php/20130729/39382.html

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.