PHP Verification Code

Source: Internet
Author: User

<?
Header ("Content-Type: image/PNG ");
// Define the header and declare the image file, preferably PNG, without copyright concerns;
$ Authnum = "";
// Define the number to be displayed to generate a new four-digit integer Verification Code
$ STR = 'abcdefghijkmnpqrstuvwxyz1234567890 ';
// Define the numbers and letters used to display on the image;
$ L = strlen ($ Str); // obtain the string length;
// Cyclically and randomly extract four letters and numbers defined above;
For ($ I = 1; $ I <5; $ I ++)
{
$ Num = rand (0 L-1 );
// A number is randomly selected at a time. From the first word to the maximum length of the string,
// Minus 1 is because the truncation character starts from 0, so that any 34 characters may be listed in it;
$ Authnum. = $ STR [$ num];
// Connects the characters obtained from numbers to a total of four digits;
}
Setcookie ("authnum", $ authnum );
// Store four digits and letters in the cookie named authnum;

// Generate the verification code image,
Srand (double) microtime () * 1000000 );
$ Im = imagecreate (); // image width and height;
// Black, white, and gray colors are used;
$ Black = imagecolorallocate ($ im, 255,255,255 );
$ White = imagecolorallocate ($ im, 218,133, 82 );
$ Gray = imagecolorallocate ($ im, 200,200,200 );
// Comment out the line without interfering with the line;
// $ Li = imagecolorallocate ($ im, 120,220,200 );
$ Li = imagecolorallocate ($ im, 0, 0 );
For ($ I = 0; $ I <3; $ I ++)
{// Add three interference lines; or do not; depending on the situation, because it may affect user input;
Imageline ($ im, Rand (), Rand (), Rand (20, 60), Rand (), $ Li );
}
// Print the four-digit integer verification code into the image
Imagefill ($ im, 272,120, $ gray );
// The Position of the character in the image;
Imagestring ($ im, 5, 14, 3, $ authnum, $ white );
For ($ I = 0; $ I <90; $ I ++)
{// Add interference pixels
Imagesetpixel ($ im, Rand () % 70, Rand () % 30, $ gray );
}
Imagepng ($ IM );
Imagedestroy ($ IM );
?>

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.