Note: add the public page to session_start (); & lt ;? Phpsession_start (); ini_set (& amp; #39; display_errors & amp; #39;, & amp; #39; Off & amp; #39 ;); classCCheckCodeFile {// Number of digits of the verification code www.2cto. comva
Note: add the public page to session_start ();
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:
Verification code
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 enter the verification code again in seconds and you will be redirected to the previous page ";
}
From http://itsafe.org /? P = 384