Php verification code example

Source: Internet
Author: User
Tags image identifier
To enhance the security of user logon, we add verification codes on the user authentication page, such as registration and logon. The verification code uses GD to generate a PNG image, and assigns $ randval random numbers to $ _ SESSION ['login _ check_num ']. the verification code is compared with $ _ POST, to determine whether it is correct. "> <LINKhref =" ht

To enhance the security of user logon, we add verification codes on the user authentication page, such as registration and logon.
The verification code uses GD to generate a PNG image, and assigns $ randval random numbers to $ _ SESSION ['login _ check_num ']. the verification code is compared with $ _ POST, to determine whether it is correct. To achieve the required functions, you need to modify the php. ini file so that php supports the GD Library.
// Call this page. if the following formula is set up, a verification code image is generated.
If ($ _ GET ["action"] = "verifycode ")
{
Rand_create ();
}
// Verification code image generation
Function rand_create ()
{
// Notify the browser that a PNG image will be output
Header ("Content-type: image/PNG ");
// Prepare the random number generator seeds
Srand (double) microtime () * 1000000 );
// Prepare image parameters
$ Im = imagecreate (62,20 );
$ Black = ImageColorAllocate ($ im, 0); // RGB black identifier
$ White = ImageColorAllocate ($ im, 255,255,255); // RGB white identifier
$ Gray = ImageColorAllocate ($ im, 200,200,200); // RGB gray identifier
// Start plotting
Imagefill ($ im, 0, 0, $ gray );
While ($ randval = rand () % 100000) <10000 );{
$ _ SESSION ["login_check_num"] = $ randval;
// Print the four-digit integer verification code into the image
Imagestring ($ im, 5, 10, 3, $ randval, $ black );
}
// Add interference pixels
For ($ I = 0; I I <200; $ I ++ ){
$ Randcolor = ImageColorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ im, rand () % 70, rand () % 30, $ randcolor );
}
// Output verification image
ImagePNG ($ im );
// Destroy the image identifier
ImageDestroy ($ im );
}
// Verify the verification code
Function rand_check ()
{
If ($ _ POST ["reg_rand"] = $ _ SESSION ["login_check_num"]) {
Return true;
}
Else {
Exit ("incorrect verification code ");
}
}
?>
 

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.