The ValidateCode. class. php code cannot be displayed in the php verification code image.
// Verification code
Class ValidateCode {
Private $ charset = 'abcdefghkmnprstuvwxyzabcdefghkmnprstuvwxyz23456789 '; // random factor
Private $ code; // verification code
Private $ codelen = 4; // verification code length
Private $ width = 130; // width
Private $ height = 50; // height
Private $ img; // graphical resource handle
Private $ font; // The specified font.
Private $ fontsize = 20; // specify the font size
Private $ fontcolor; // specifies the font color.
// Constructor initialization
Public function _ construct (){
$ This-> font = dirname (_ FILE _). '/font/elephant. ttf'; // note that the font path must be correct; otherwise, the image cannot be displayed.
}
// Generate random code
Private function createCode (){
$ _ Len = strlen ($ this-> charset)-1;
For ($ I = 0; $ I <$ this-> codelen; $ I ++ ){
$ This-> code. = $ this-> charset [mt_rand (0, $ _ len)];
}
}
// Generate background
Private function createBg (){
$ This-> img = imagecreatetruecolor ($ this-> width, $ this-> height );
$ Color = imagecolorallocate ($ this-> img, mt_rand (157,255), mt_rand (157,255), mt_rand (157,255 ));
Imagefilledrectangle ($ this-> img, 0, $ this-> height, $ this-> width, 0, $ color );
}
// Generate text
Private function createFont (){
$ _ X = $ this-> width/$ this-> codelen;
For ($ I = 0; $ I <$ this-> codelen; $ I ++ ){
$ This-> fontcolor = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156 ));
Imagettftext ($ this-> img, $ this-> fontsize, mt_rand (-30, 30), $ _ x * $ I + mt_rand (1.4), $ this-> height, $ this-> fontcolor, $ this-> font, $ this-> code [$ I]);
}
}
// Generate lines and snowflakes
Private function createLine (){
// Line
For ($ I = 0; $ I <6; $ I ++ ){
$ Color = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156 ));
Imageline ($ this-> img, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), $ color );
}
// Snowflake
For ($ I = 0; I I <100; $ I ++ ){
$ Color = imagecolorallocate ($ this-> img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
Imagestring ($ this-> img, mt_rand (1, 5), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height ),'*', $ color );
}
}
// Output
Private function outPut (){
Header ('content-type: image/png ');
Imagepng ($ this-> img );
Imagedestroy ($ this-> img );
}
// Generate externally
Public function doimg (){
$ This-> createBg ();
$ This-> createCode ();
$ This-> createLine ();
$ This-> createFont ();
$ This-> outPut ();
}
// Obtain the verification code
Public function getCode (){
Return strtolower ($ this-> code );
}
}
The captcha. php code is as follows:
Session_start ();
Require './ValidateCode. class. php'; // first include the class, and modify the actual path according to the actual situation.
$ _ Vc = new ValidateCode (); // instantiate an object
$ _ Vc-> doimg ();
$ _ SESSION ['authnum _ session'] =$ _ vc-> getCode (); // the verification code is saved to the session.
The index. php code is as follows:
Session_start ();
// Enable the session first at the top,
// Error_reporting (2047 );
Session_destroy ();
// Remove the session to get the new session value every time;
// Use seesion for good results and convenience
?>
Session image verification instance
// Print the previous session;
Echo "previous session:
". $ _ SESSION [" authnum_session "]."
";
$ Validate = "";
If (isset ($ _ POST ["validate"]) {
$ Validate = $ _ POST ["validate"];
Echo "you just entered:". $ _ POST ["validate"]."
Status :";
If ($ validate! = $ _ SESSION ["authnum_session"]) {
// Determine whether the session value is consistent with the verification code entered by the user;
Echo "incorrect input ";
} Else {
Echo "verified ";
}
}
?>
Answers from beginners
Reply to discussion (solution)
Image after running
Run captcha. php separately.
1. ensure that captcha. php does not have a BOM header
2. check whether the font file exists.
I saw this code and debugged it two days ago .. It should be about the font path.
'/Font/elephant. ttf
All right, ValidateCode. class. php and captcha. php are changed to no BOM header at the same time, so pay attention to the font file.
However, I found another problem. I don't want to post any more. Which of the following is a great question?