Verification Code Generation Program and usage

Source: Internet
Author: User

The Verification Code Generation Program and usage method below is a verification code generated using php. The srand function is used to generate the verification image and use the image function of the php gd library to generate the verification image, then save the generated data to the global variables of the session.

Verification Code Generation Program and usage

The following is a verification code program generated using the php tutorial. It uses the srand arbitrary function generation and uses the image function of the php gd library to generate the verification image, and then saves the generated data to the session global variable.

// 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 );{
// Print the four-digit integer verification code into the image
Session_start ();
$ _ Session ['login _ check_num '] = $ randval;
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 );
}
?>


Call Method

Check whether the verification code is entered correctly

<?
If ($ _ post ['code'] ==$ _ session ['login _ check_num '])
{
Echo 'correct Verification Code ';
}
Else
{
Echo '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.