PHP 出力セッションの検証コードが画像と同期していません。画像は常に 1 つ先を行っています。解決してください。

WBOY
リリース: 2016-06-23 14:05:51
オリジナル
983 人が閲覧しました

PHP 出力セッションの検証コードが画像と同期していません。画像は常に 1 つ先にあります。解決してください。

<?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);}$str=random(4); $width = 50; $height = 25; @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;?>
ログイン後にコピー


<img src="captcha.php" width="60" height="22" border="1" onclick= "this.src='captcha.php?act=captcha&'+Math.random()" style="cursor: pointer; vertical-align:middle" title="看不清?点击更换!" />
ログイン後にコピー


ディスカッションへの返信 (解決策)

PHP セッション出力の検証コードは画像と同期していません。画像は常に 1 ステップ速くなります
これはどういう意味ですか?
コード内で $_SESSION["captcha"] に値を繰り返し割り当てる必要があるのはなぜですか?

PHP 出力セッション検証コードと画像が同期していません。画像は常に 1 歩先を進んでいます
これは何を意味しますか?
コード内で $_SESSION["captcha"] に値を繰り返し割り当てる必要があるのはなぜですか?

削除しても、画像は依然として echo よりも一歩早いです

この問題は、分離するのが最善の方法です。
これを試してください
checkcode.class.php

<?php/** * 生成验证码 * 类用法 * $checkcode = new checkcode(); * $checkcode->doimage(); * //取得验证 * $_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 {	//验证码的宽度	public $width=130;		//验证码的高	public $height=50;		//设置字体的地址	private $font;		//设置字体色	public $font_color;		//设置随机生成因子	public $charset = 'abcdefghkmnprstuvwyzABCDEFGHKLMNPRSTUVWYZ23456789';		//设置背景色	public $background = '#EDF7FF';		//生成验证码字符数	public $code_len = 4;		//字体大小	public $font_size = 20;		//验证码	private $code;		//图片内存	private $img;		//文字X轴开始的地方	private $x_start;			function __construct($fontpath) {		$this->font =$fontpath;	}	/**	 * 生成随机验证码。	 */	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;	}		/**	 * 获取验证码	 */	public function get_code() {		return strtolower($this->code);	}		/**	 * 生成图片	 */	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, 1,2)), hexdec(substr($this->font_color, 3,2)), hexdec(substr($this->font_color, 5,2)));		}		//设置背景色		$background = imagecolorallocate($this->img,hexdec(substr($this->background, 1,2)),hexdec(substr($this->background, 3,2)),hexdec(substr($this->background, 5,2)));		//画一个柜形,设置背景颜色。		imagefilledrectangle($this->img,0, $this->height, $this->width, 0, $background);		$this->creat_font();		$this->creat_line();		$this->output();	}		/**	 * 生成文字	 */	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;		}	}		/**	 * 画线	 */	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 * .75, $ypos, $width, $height, $start, $end, $this->font_color);	}		/**	 * 输出图片	 */	private function output() {		header("content-type:image/png\r\n");		imagepng($this->img);		imagedestroy($this->img);	}}
ログイン後にコピー


getimg.php

<?phpsession_start();include './checkcode.class.php';$checkcode = new checkcode('C:\WINDOWS\Fonts\ARIAL.TTF');$checkcode->doimage();$_SESSION['code']=$checkcode->get_code();
ログイン後にコピー

test.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /></head><body > <form action="submit.php" method="post" name="myform">	<img id='code_img'   onclick='this.src=this.src+"&"+Math.random()' src='getimg.php'/> 	<input name="code" type="text" class="ipt ipt_reg"  />	<input name="submit" value="提交" type="submit"/> </form></body></html>	
ログイン後にコピー


submit.php
<?phpsession_start();var_dump($_SESSION);echo  "<HR/>";var_dump($_REQUEST);
ログイン後にコピー

もちろん

A.php 出力


A をリクエストしているブラウザ.php, SESSION が出力される

そして、ページ A をレンダリングするときに画像ノードが見つかり、captcha.php をリクエストすると、サーバーが検証画像を生成し、この時に新しい SESSION 値が生成される

ということのようです。まだ問題ないはずです

ブラウザにページが 2 回読み込まれる原因となるのは、ブラウザにインストールされているデバッグ プラグインに問題がありますか?

これにより、ブラウザはページを 2 回ロードするため、確認コードも 2 回ロードされます

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!