PHP generates image verification codes and click to switch instances. Let's take a look at the following results: phpsession_start (); functionrandom ($ len) {$ srcstr1a2s3d4f5g6hj8k9qwertyupzxcvbnm; mt_s:
Now let's take a look at the PHP code.
The 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 );
// The image width of the verification code
$ Width = 50;
// Height of the verification code Image
$ Height = 25;
// Declare the image format of the layer to be created
@ Header ("Content-Type: image/png ");
// Create a layer
$ Im = imagecreate ($ width, $ height );
// Background color
$ Back = imagecolorallocate ($ im, 0xFF, 0xFF, 0xFF );
// Blur the vertex color
$ Pix = imagecolorallocate ($ im, 187,230,247 );
// Font color
$ Font = imagecolorallocate ($ im, 41,163,238 );
// Plot the vertices of fuzzy match.
Mt_srand ();
For ($ I = 0; I I <1000; $ I ++ ){
Imagesetpixel ($ im, mt_rand (0, $ width), mt_rand (0, $ height), $ pix );
}
// Output characters
Imagestring ($ im, 5, 7, 5, $ str, $ font );
// Output rectangle
Imagerectangle ($ im, 0, 0, $ width-1, $ height-1, $ font );
// Output image
Imagepng ($ im );
Imagedestroy ($ im );
$ Str = md5 ($ str );
// Select cookie
// SetCookie ("verification", $ str, time () + 7200 ,"/");
// Select Session
$ _ SESSION ["verification"] = $ str;
?>
Next, you just need to call it on the page:
The code is as follows:
If you want to achieve "cannot see clearly? For a different effect, add the following JS to the page.
The code is as follows:
Function changing (){
Document. getElementById ('checkpic '). src = "/images/checkcode. php? "+ Math. random ();
}
Now let's take a look at the PHP code: php session_start (); function random ($ len) {$ srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm"; mt_s...