As follows:
Copy codeThe Code is as follows: <? Php
$ Rndstring = '';
For ($ I = 0; $ I <4; $ I ++) $ rndstring. = chr (mt_rand (65,90 ));
// If GD is supported, draw
If (function_exists ("imagecreate "))
{
// In some cases in Firefox, there will be multiple requests, and refreshing the page within 5 seconds will not change the session
$ Ntime = time ();
If (empty ($ _ SESSION ['dd _ ckstr_last ']) | empty ($ _ SESSION ['dd _ ckstr']) | ($ ntime-$ _ SESSION ['dd _ ckstr_last ']> 5 ))
{
$ _ SESSION ['dd _ ckstr'] = strtolower ($ rndstring );
$ _ SESSION ['dd _ ckstr_last '] = $ ntime;
}
$ Rndstring = $ _ SESSION ['dd _ ckstr'];
$ Rndcodelen = strlen ($ rndstring );
// Create an image and set the background color
$ Im = imagecreate (50, 20 );
ImageColorAllocate ($ im, 255,255,255 );
// Background line
$ LineColor1 = ImageColorAllocate ($ im, 240,220,180 );
$ LineColor2 = ImageColorAllocate ($ im, 250,250,170 );
For ($ j = 3; $ j <= 16; $ j = $ j + 3)
{
Imageline ($ im, 2, $ j, 48, $ j, $ lineColor1 );
}
For ($ j = 2; $ j <52; $ j = $ j + (mt_rand (3, 6 )))
{
Imageline ($ im, $ j, 2, $ j-6, 18, $ lineColor2 );
}
// Draw a border
$ Bordercolor = ImageColorAllocate ($ im, 0x99,0x99,0x99 );
Imagerectangle ($ im, 0, 0, 49, 19, $ bordercolor );
// Output text
$ FontColor = ImageColorAllocate ($ im, 48, 61, 50 );
For ($ I = 0; $ I <$ rndcodelen; $ I ++)
{
$ Bc = mt_rand (0, 1 );
$ Rndstring [$ I] = strtoupper ($ rndstring [$ I]);
Imagestring ($ im, 5, $ I * 10 + 6, mt_rand (2, 4), $ rndstring [$ I], $ fontColor );
}
Header ("Pragma: no-cache/r/n ");
Header ("Cache-Control: no-cache/r/n ");
Header ("Expires: 0/r/n ");
// Output a specific image format with a priority of gif-> jpg-> png
If (function_exists ("imagejpeg "))
{
Header ("content-type: image/jpeg/r/n ");
Imagejpeg ($ im );
}
Else
{
Header ("content-type: image/png/r/n ");
Imagepng ($ im );
}
ImageDestroy ($ im );
Exit ();
}
Else
{
// GD is not supported. Only the English letter ABCD is output.
$ _ SESSION ['dd _ ckstr'] = "abcd ";
$ _ SESSION ['dd _ ckstr_last '] = '';
Header ("content-type: image/jpeg/r/n ");
Header ("Pragma: no-cache/r/n ");
Header ("Cache-Control: no-cache/r/n ");
Header ("Expires: 0/r/n ");
$ Fp = fopen ("data/vdcode.jpg", "r ");
Echo fread ($ fp, filesize ("data/vdcode.jpg "));
Fclose ($ fp );
Exit ();
}
?>