PHP generates a verification code with a snowflake background _ PHP Tutorial

Source: Internet
Author: User
PHP generates a verification code with a snowflake background. For more information about the effect, see [source code] mail. htjs. netyanzhengmatest. php? Session_start ();? FORMMETHODPOSTACTIONinputtypetextnamenumbermaxlength4imgsrcYanZhengMa. php? Acti effect reference (source code) http://mail.htjs.net/yanzhengma/test.php



// Check the verification code
If (isset ($ HTTP_POST_VARS ["sub"]):
If ($ HTTP_POST_VARS ["number"]! = $ HTTP_SESSION_VARS [login_check_number] | empty ($ HTTP_POST_VARS ["number"]) {
Echo "incorrect verification code! ";
} Else {
Echo "verification code passed! ";
}
Endif;
Show_source ('test. php ');
// Source code on the above page


// The source code for generating the verification code is as follows:
Show_source ('yanzhengma. php ');
?>
Session_start ();
Session_register ("login_check_number ");
// I saw the verification code on chianren last night. I thought about it and used the PHP GD library to complete similar functions.
// First generate the background, and then put the generated verification code.
$ Img_height = 120; // define the length and width of the image first.
$ Img_width = 40;
If ($ HTTP_GET_VARS ["act"] = "init "){
// Srand (microtime () * 100000); // srand is not required after PHP420
For ($ Tmpa = 0; $ Tmpa <4; $ Tmpa ++ ){
$ Nmsg. = dechex (rand (0, 15 ));
} // By sports98


$ HTTP_SESSION_VARS [login_check_number] = $ nmsg;

// $ HTTP_SESSION_VARS [login_check_number] = strval (mt_rand ("1111", "9999"); // generates a four-digit random number and puts it into the session
// Who can make the supplement and generate letters and numbers at the same time ?? ---- Completed by sports98

$ Aimg = imageCreate ($ img_height, $ img_width); // generate an image
ImageColorAllocate ($ aimg, 255,255,255); // image background color, ImageColorAllocate 1st definition color PHP considers it as background color
$ Black = ImageColorAllocate ($ aimg, 0); // define the required black
ImageRectangle ($ aimg, $ img_height-1, $ img_width-1, $ black); // enclose an image in a black rectangle first

// The following generates a snowflake background. In fact, some symbols are generated on the image.
For ($ I = 1; $ I <= 100; $ I ++) {// test with 100
ImageString ($ aimg, 1, mt_rand (1, $ img_height), mt_rand (1, $ img_width), "*", imageColorAllocate ($ aimg, mt_rand (200,255 ), mt_rand (200,255), mt_rand (200,255 )));
// Ha, you can see it. it's not actually a snowflake, it's just generating the * number. To make them look "chaotic, 5 colors and 6 colors", you have to make their positions, colors, and even sizes use random numbers when one of them is generated, rand () or mt_rand can be completed.
}

// The background is generated above. now we should put the generated random number. The principle is similar to the above. Random numbers are placed in one place, and their positions, sizes, and colors are used as random numbers ~~
// To distinguish it from the background, the color here shall not exceed 200, and the color above shall not be less than 200
For ($ I = 0; $ I ImageString ($ aimg, mt_rand (3, 5), $ I * $ img_height/4 + mt_rand (1, 10), mt_rand (1, $ img_width/2 ), $ HTTP_SESSION_VARS [login_check_number] [$ I], imageColorAllocate ($ aimg, mt_rand (0,100), mt_rand (0,150), mt_rand (0,200 )));
}
Header ("Content-type: image/png"); // tell the browser that the following data is an image instead of text
ImagePng ($ aimg); // Generate the png format... Hey, the effect is pretty similar...
ImageDestroy ($ aimg );
}

?>

Lightning (with source code) http://mail.htjs.net/yanzhengma/test.php? Session_start ();? Form method = post action = "" input type = text name = number maxlength = 4img src = "YanZhengMa. php? Act = I...

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.