Effect reference (with source code) http://mail.htjs.net/yanzhengma/test.php
<? Session_start ();?>
<Form method = post action = "">
<Input type = text name = number maxlength = 4>
<Input type = "submit" name = "sub">
</FORM>
<?
// 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 ');
?>
<? 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 <strlen ($ HTTP_SESSION_VARS [login_check_number]); $ 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 );
}
?>