PHP output session verification code and the picture is not the same step, the picture is always one step faster, solve! Solution Solutions

Source: Internet
Author: User
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-&GT;HEIGHT/2-Rand (10, 30); $xpos+ = rand (5, 15);          } else {$start = rand (180, 246);        $ypos = $this-&GT;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-&GT;HEIGHT/2-Rand (10, 30);        $xpos + = rand (5, 15);          } else {$start = rand (200, 250);        $ypos = $this-&GT;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);  }}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.