PHP output session verification code and the picture is not the same step, the picture is always one step faster, solve!
PHP output session verification code and the picture is not the same step, the picture is always one step faster, solve!
PHP Code
PHP Code
------Solution--------------------
The problem is very strange, the best way is to separate.
Try this one.
checkcode.class.php
PHP Code
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 {//captcha width public $width = 130; High public $height of the CAPTCHA = 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 number of Captcha characters public $code _len = 4; Font size public $font _size = 20; Verification code Private $code; Image memory Private $img; The text x-axis begins where the private $x _start; function __construct ($fontpath) {$this->font = $fontpath; }/** * Generates a 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); }/** * Generate picture */Public Function doimage () {$code = $this->creat_code (); $this->img = Imagecreatetruecolor ($this->width, $this->height); if (! $this->font_color) {$this->font_color = imagecolorallocate ($this->img, rand (0,156), Rand (0,156), Rand (0,156)); } else {$this->font_color = imagecolorallocate ($this->img, Hexdec (substr ($this->font_color,)), he Xdec (substr ($this->font_color, 3,2)), Hexdec (substr ($this->font_color, 5,2))); }//Set background color $background = imagecolorallocate ($this->img,hexdec (substr ($this->background, Up)), Hexdec (su BSTR ($this->background, 3,2)), Hexdec (substr ($this->background, 5,2)); Draw a Cabinet shape, setBackground color. Imagefilledrectangle ($this->img,0, $this->height, $this->width, 0, $background); $this->creat_font (); $this->creat_line (); $this->output (); }/** * Generates text */Private function Creat_font () {$x = $this->width/$this->code_len; for ($i =0; $i < $this->code_len; $i + +) {Imagettftext ($this->img, $this->font_size, Rand ( -30,30), $x * $i +rand (0,5), $this->height/1.4, $this->font_color, $this->font, $this->code[$i]); if ($i ==0) $this->x_start= $x * $i +5; }}/** * Draw line * */Private Function Creat_line () {imagesetthickness ($this->img, 3); $xpos = ($this->font_size * 2) + rand (-5, 5); $width = $this->width/2.66 + rand (3, 10); $height = $this->font_size * 2.14; if (rand (0,100)% 2 = = 0) {$start = rand (0,66); $ypos = $this->HEIGHT/2-Rand (10, 30); $xpos+ = rand (5, 15); } else {$start = rand (180, 246); $ypos = $this->HEIGHT/2 + rand (10, 30); } $end = $start + rand (75, 110); Imagearc ($this->img, $xpos, $ypos, $width, $height, $start, $end, $this->font_color); if (rand (1,75)% 2 = = 0) {$start = rand (45, 111); $ypos = $this->HEIGHT/2-Rand (10, 30); $xpos + = rand (5, 15); } else {$start = rand (200, 250); $ypos = $this->HEIGHT/2 + rand (10, 30); } $end = $start + rand (75, 100); Imagearc ($this->img, $this->width *, $ypos, $width, $height, $start, $end, $this->font_color); }/** * Output picture */Private Function output () {header ("content-type:image/png\r\n"); Imagepng ($this->img); Imagedestroy ($this->img); }}