PHP output session verification code and the picture is not the same step, the picture is always one step faster, solve!
This post was last edited by Xdawei on 2012-04-18 20:07:42
PHP output Session verification code and the picture is not the same step, the picture is always one step faster, solve!
Session_Start ();
function Random ($len)
{
$srcstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Mt_srand ();
$strs = "";
for ($i =0; $i < $len; $i + +) {
$strs. = $srcstr [Mt_rand (0,35)];
}
Return Strtoupper ($STRS);
}
@header ("Content-type:image/png");
$_session["Captcha"] = $str;
Echo $str;
$im =imagecreate ($width, $height);
$back =imagecolorallocate ($im, 0xff,0xff,0xff);
$pix =imagecolorallocate ($im, 187,230,247);
$font =imagecolorallocate ($im, 41,163,238);
Mt_srand ();
for ($i =0; $i <1000; $i + +)
{
Imagesetpixel ($im, Mt_rand (0, $width), Mt_rand (0, $height), $pix);
}
Imagestring ($im, 5, 7, 5, $STR, $font);
Imagerectangle ($im, 0,0, $width-1, $height-1, $font);
Imagepng ($im);
Imagedestroy ($im);
$_session["Captcha"] = $str;
?>
------Solution--------------------
The problem is very strange, the best way is to separate.
Try this one.
checkcode.class.php
/**
* Generate Verification Code
* Class Usage
* $checkcode = new Checkcode ();
* $checkcode->doimage ();
*//Get Verification
* $_session[' code ']= $checkcode->get_code ();
Session_Start ();
Include './checkcode.class.php ';
$checkcode = new Checkcode (' C:\WINDOWS\Fonts\ARIAL. TTF ');
$checkcode->doimage ();
$_session[' code ']= $checkcode->get_code ();
*/
Class Checkcode {
The width of the verification code
Public $width = 130;
Verification Code of the high
Public $height = 50;
Set the address of the font
Private $font;
Set the font color
Public $font _color;
Set the random generation factor
Public $charset = ' abcdefghkmnprstuvwyzABCDEFGHKLMNPRSTUVWYZ23456789 ';
Set Background color
Public $background = ' #EDF7FF ';
Generate Verification Code character count
Public $code _len = 4;
Font size
Public $font _size = 20;
Verification Code
Private $code;
Image memory
Private $img;
Where the text x-axis begins
Private $x _start;
function __construct ($fontpath) {
$this->font = $fontpath;
}
/**
* Generate random verification code.
*/
protected function Creat_code () {
$code = ";
$charset _len = strlen ($this->charset)-1;
for ($i =0; $i < $this->code_len; $i +) {
$code. = $this->charset[rand (1, $charset _len)];
}
$this->code = $code;
}
/**
* Get Verification Code
*/
Public Function Get_code () {
Return Strtolower ($this->code);
}
/**
* Create picture
*/
Public Function Doimage () {
$code = $this->creat_code ();
$this->img = Imagecreatetruecolor ($this->width, $this->height);
if (! $this->font_color) {