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);
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.