Note: add the public page to session_start ();
<? Php
Session_start ();
Ini_set ('display _ errors ', 'off ');
Class CCheckCodeFile
{
// Number of digits of the Verification Code www.2cto.com
Var $ mCheckCodeNum = 4;
// Generated Verification Code
Var $ mCheckCode = '';
// Verification Code Image
Var $ mCheckImage = '';
// Interference Pixel
Var $ mDisturbColor = '';
// The image width of the Verification Code
Var $ mCheckImageWidth = '80 ';
// The image width of the Verification Code
Var $ mCheckImageHeight = '20 ';
// Output Header
Function OutFileHeader ()
{
Header ("Content-type: image/png ");
}
// Generate a verification code
Function CreateCheckCode ()
{
// $ This-> mCheckCode = strtoupper (substr (md5 (rand (), 0, $ this-> mCheckCodeNum ));
$ This-> mCheckCode = strtoupper (substr (rand (0,999999999999), 0, $ this-> mCheckCodeNum ));
Session_cache_expire (60 );
Session_start ();
$ _ SESSION ["code_str"] = $ this-> mCheckCode;
Return $ this-> mCheckCode;
}
// Generate a verification code Image
Function CreateImage ()
{
$ This-> mCheckImage = @ imagecreate ($ this-> mCheckImageWidth, $ this-> mCheckImageHeight );
Imagecolorallocate ($ this-> mCheckImage, 255,255,255 );
Return $ this-> mCheckImage;
}
// Sets the interference pixels of the image.
Function SetDisturbColor ()
{
For ($ I = 0; $ I <= 128; $ I ++)
{
$ This-> mDisturbColor = imagecolorallocate ($ this-> mCheckImage, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ this-> mCheckImage, rand (1,100), rand (1,100), $ this-> mDisturbColor );
}
}
// Set the size, width, and height of the Verification Code image.
Function SetCheckImageWH ($ width, $ height)
{
If ($ width = ''| $ height ='') return false;
$ This-> mCheckImageWidth = $ width;
$ This-> mCheckImageHeight = $ height;
Return true;
}
// Upload the verification code one by one on the Verification Code Image
Function WriteCheckCodeToImage ()
{
For ($ I = 0; $ I <= $ this-> mCheckCodeNum; $ I ++)
{
$ Bg_color = imagecolorallocate ($ this-> mCheckImage, rand (0,255), rand (0,255), rand (0,255 ));
$ X = floor ($ this-> mCheckImageWidth/$ this-> mCheckCodeNum) * $ I;
$ Y = rand (0, $ this-> mCheckImageHeight-15 );
Imagechar ($ this-> mCheckImage, 5, $ x, $ y, $ this-> mCheckCode [$ I], $ bg_color );
}
}
// Output the verification code Image
Function OutCheckImage ()
{
$ This-> OutFileHeader ();
$ This-> CreateCheckCode ();
$ This-> CreateImage ();
$ This-> SetDisturbColor ();
$ This-> WriteCheckCodeToImage ();
Imagepng ($ this-> mCheckImage );
Imagedestroy ($ this-> mCheckImage );
}
}
$ C_check_code_image = new CCheckCodeFile ();
// $ C_check_code_image-> SetCheckImageWH (, 50); // you can specify the image size of the verification code.
$ C_check_code_image-> OutCheckImage ();
?>
Usage:
<Tr>
<Td align = "center" valign = "middle"> Verification Code </td>
<Td align = "center" valign = "middle"> <input name = "number" type = "text" style = "width: 80px;">
</Td>
</Tr>
Verify if it is correct
Session_start ();
If ($ _ POST ["number"] ==$ _ SESSION ["code_str"]) {
// Echo ("the verification code is correct ");
} Else {
Echo"
<Script>
Var s = 10;
Function OK ()
{
S --;
Document. getElementById ('tids'). innerHTML = s;
If (s = 0)
{
ClearTimeout (t );
History. go (-1 );
}
}
Var t = setInterval ('OK ()', 1000 );
</Script>
". "Enter the verification code. Please re-enter <span id = 'tids'> </span> seconds to jump to <a href = \" javascript: history. go (-1) \ "> previous page </a> ";
}
From http://itsafe.org /? P = 384