Http://www.jb51.net/article/51506.htm
Now let's take a look at the PHP code
Copy CodeThe code is as follows:
<?php
Session_Start ();
function Random ($len) {
$srcstr = "1A2S3D4F5G6HJ8K9QWERTYUPZXCVBNM";
Mt_srand ();
$strs = "";
for ($i = 0; $i < $len; $i + +) {
$strs. = $srcstr [Mt_rand (0, 30)];
}
return $strs;
}
Randomly generated string
$STR = random (4);
Verify the width of the code picture
$width = 50;
Verify the height of the code picture
$height = 25;
Declare the picture format of the layer you want to create
@ Header ("Content-type:image/png");
Create a layer
$im = Imagecreate ($width, $height);
Background color
$back = Imagecolorallocate ($im, 0xFF, 0xFF, 0xFF);
Blur Point Color
$pix = Imagecolorallocate ($im, 187, 230, 247);
Font Color
$font = Imagecolorallocate ($im, 41, 163, 238);
The point of painting blur function
Mt_srand ();
for ($i = 0; $i < $i + +) {
Imagesetpixel ($im, Mt_rand (0, $width), Mt_rand (0, $height), $pix);
}
Output character
Imagestring ($im, 5, 7, 5, $STR, $font);
Output Rectangle
Imagerectangle ($im, 0, 0, $width-1, $height-1, $font);
Output picture
Imagepng ($im);
Imagedestroy ($im);
$STR = MD5 ($STR);
Select a cookie
Setcookie ("Verification", $str, Time () + 7200, "/");
Select Session
$_session["verification"] = $STR;
?>
The next thing you can do is call it in the page:
Copy CodeThe code is as follows:
If you want to realize "can't see?" Change the "effect, add the following JS to the page
Copy CodeThe code is as follows:
function changing () {
document.getElementById (' Checkpic '). src= "/images/checkcode.php?" +math.random ();
}
PHP Generate image Verification code, click to switch instances