PHP generates a verification code with a snowflake background

Source: Internet
Author: User
Results reference (with source code) http://mail.htjs.net/yanzhengma/test.php (as the current mainstream development language) & lt ;? Session_start ();? & Gt; & lt; forpolichodpostaction & gt; & lt; inputtypetextnamenumbermaxlength4 & gt; & lt

Effect reference (with source code) http://mail.htjs.net/yanzhengma/test.php (as the current mainstream development language)



// 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 (as the mainstream development language ));
// Source code on the above page


// The source code for generating the verification code is as follows:
Show_source (YanZhengMa. php (as the mainstream development language ));
?>
Session_start ();
Session_register ("login_check_number ");
// I saw the verification code on chianren last night. I considered it and used php (as the mainstream development language) 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); // after php (as the mainstream development language) is 420, srand is not required
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 (as the mainstream development language) is considered 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

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.