Verification Code
PHP generates a verification code with a snowflake background
Verification Check Code
if (Isset ($HTTP _post_vars["sub")):
if ($HTTP _post_vars["number"]! = $HTTP _session_vars[login_check_number]
Empty ($HTTP _post_vars["number"]) {
echo "Check code is incorrect!";
}else{
echo "Verification code passed! ";
}
endif
Show_source (' test.php ');
The source code of the above page
Here is the source code to generate the verification code
Show_source (' yanzhengma.php ');
?>
Session_Start ();
Session_register ("Login_check_number");
Last night saw the Chianren on the Verification code effect, considered a bit, with the PHP GD library to complete similar functions
First genetic the background, and then the generated verification on the stacking up
$img _height=120; Define the length and width of the image first.
$img _width=40;
if ($HTTP _get_vars["act"]== "init") {
Srand (Microtime () * 100000);//php420, 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")); Generate 4-bit random numbers and put them in session
Who can make the next addition, you can generate letters and numbers at the same time?? ----finished by Sports98.
$aimg = Imagecreate ($img _height, $img _width); Create a picture
Imagecolorallocate ($aimg, 255,255,255); Image background, imagecolorallocate the 1th time definition of color PHP is considered an undertone.
$black = Imagecolorallocate ($aimg, 0,0,0); Define the required Black
Imagerectangle ($aimg, 0,0, $img _height-1, $img _width-1, $black);//First a black rectangle surrounds the picture
It's time to create a snowflake background, which is actually creating some symbols on the image.
for ($i =1; $i <=100; $i + +) {//test with 100 first
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, see, in fact, is not a snowflake, is to generate * number just. To make them look "disorganized, 5 6", you have to generate them in 1 1, with random numbers for their position, color, and even size, which rand () or Mt_rand can do.
}
The background is generated and it is time to put the random numbers that have been generated. The reason is similar to above, random number 1 1 place, at the same time let their position, size, color are used in random number ~ ~
To distinguish it from the background, the color here is not more than 200, the above is not 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 a picture, not a text display
Imagepng ($AIMG); Generate PNG format ... Hey, the effect is pretty much like a thing ...
Imagedestroy ($AIMG);
}
?>