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

Source: Internet
Author: User
Tags set background
PHP output session verification code and the picture is not the same step, the picture is always one step faster, solve!

 
  



Reply to discussion (solution)

PHP output session verification code and the picture is not the same step, the picture is always one step ahead
What does that mean?
Why do you want to repeat the assignment of $_session["Captcha" in your code?

PHP output session verification code and the picture is not the same step, the picture is always one step ahead
What does that mean?
Why do you want to repeat the assignment of $_session["Captcha" in your code?

Get rid of the same picture or one step faster than echo

The problem is very strange, the best way is to separate.
Try this one.
checkcode.class.php

 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;//authentication Code high public $height =50;//set the address of the font to private $font;//Set Font Color public $font _ color;//set random generate factor public $charset = ' abcdefghkmnprstuvwyzABCDEFGHKLMNPRSTUVWYZ23456789 ';//Set background color public $background = ' # EDF7FF ';//generate CAPTCHA characters public $code _len = 4;//font size public $font _size = 20;//verification Code private $code;//Picture memory private $img; The text x-axis begins where the 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);} /** * 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,)), Hexdec ( substr ($this->font_color, 3,2)), Hexdec (substr ($this->font_color, 5,2)));} Set the background color $background = imagecolorallocate ($this->img,hexdec (substr ($this->background,)), Hexdec (substr ($ This->background, 3,2)), Hexdec (substr ($this->background, 5,2)));//Draw a Cabinet shape, set the background color. Imagefilledrectangle ($this->img,0, $this->height, $this->width, 0, $background); $this->creat_font (); $ This->creat_line (); $this->output ();} /** * Generate 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 image */private function output () {header ("content-type:image/png\r\n") imagepng ($this->img); Imagedestroy ($ this->img);}}


getimg.php

 
  Doimage (); $_session[' Code ']= $checkcode->get_code ();

test.php
 
  
 
  


submit.php
 
  "; Var_dump ($_request);

Of course

a.php output


Browser request a.php, output the session

Then render a page, find the image node, and then request captcha.php, the server side and then generate a verification image, this time generated a new session value

Well, I guess we have to study hard.

is not the browser installed debugging plugin problem, so that the browser will load the page two times

is not the browser installed debugging plugin problem, so that the browser will load the page two times, so the verification code has been loaded twice

  • Related Article

    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.