This article mainly introduces how to generate an image Verification Code instance in PHP and click switch (not clear? For more information, see
This article mainly introduces how to generate an image Verification Code instance in PHP and click switch (not clear? For more information, see
The following figure shows the effect:
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 ();
}
,