Php code for generating verification Codes

Source: Internet
Author: User
Tags image identifier

This php code generation Verification Code principle generates a random number --> creates an image --> writes a random number into an image --> saves it in the session, take a look at the process verification code Image Generation notice the browser will output the PNG Image prepare the random number generator seed srand (double) microtime () * 1000000); the four-digit integer verification code is drawn into the image

/Php tutorial to generate Verification Code
/* ===================================================== ====================
This php code generation Verification Code principle generates a random number --> creates an image --> writes a random number into an image --> saves it in the session, take a look at the process verification code Image Generation notice the browser will output the png Image prepare the random number generator seed srand (double) microtime () * 1000000); the four-digit integer verification code is drawn into the image
========================================================== ===================== */

/* ================================
Generate random string functions
===================================== */
Function random ($ length ){
$ Hash = '';
$ Chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxy ';
$ Max = strlen ($ chars)-1;
Mt_srand (double) microtime () * 1000000 );
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Hash. = $ chars [mt_rand (0, $ max)];
}
Return $ hash;
}

// Verification Code Image Generation

Session_start ();
// 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,22 );
$ Black = imagecolorallocate ($ im, 0); // rgb black identifier
$ White = imagecolorallocate ($ im, 255,255,255); // rgb white identifier
$ Gray = imagecolorallocate ($ im, 179,183,185); // rgb gray identifier
// Start plotting
Imagefill ($ im, 0, 0, $ gray );
// While ($ randval = rand () % 100000) <10000 );{
// $ _ Session ["check_code"] = $ randval;
// Print the four-digit integer verification code into the image
$ Randval = random (4 );
$ _ Session ["check_code"] = $ randval;
Imagestring ($ im, 5, 10, 3, $ randval, $ white );
//}
// Add interference pixels
For ($ I = 0; I I <150; $ I ++ ){
$ Randcolor = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ im, rand () % 70, rand () % 30, $ white );
}
// Output verification Image
Imagepng ($ im );
// Destroy the image identifier
Imagedestroy ($ im );

?>

Related Article

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.