PHP generates a verification code with a snowflake background? Session_start ();? FORMMETHODPOSTACTIONinputtypetextnamenumbermaxlength4imgaltPHP generates the verification code srcwww. yeahtech. comStudyYanZhengMa. php? ActinitINPUTTYPEsubmitnamesubFORM? Verification code
PHP generates a verification code with a snowflake background
// 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 );
}
?>