目录
回复讨论(解决方案)
首页 后端开发 php教程 PHP 输出session 验证码与图片不同步,图片总是快一步,求解!

PHP 输出session 验证码与图片不同步,图片总是快一步,求解!

Jun 23, 2016 pm 02:05 PM

PHP 输出session 验证码与图片不同步,图片总是快一步,求解!

<?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="/static/imghw/default1.png"  data-src="captcha.php"  class="lazy"      style="max-width:90%"  style="max-width:90%" border="1" onclick= "this.+Math.random()" style="cursor: pointer; vertical-align:middle" title="看不清?点击更换!" / alt="PHP 输出session 验证码与图片不同步,图片总是快一步,求解!" >
登录后复制


回复讨论(解决方案)

PHP 输出session 验证码与图片不同步,图片总是快一步
这是什么意思?
你的代码中为什么要对$_SESSION["captcha"]重复赋值?

PHP 输出session 验证码与图片不同步,图片总是快一步
这是什么意思?
你的代码中为什么要对$_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  src="/static/imghw/default1.png"  data-src="getimg.php?Math.random()"  class="lazy"  id='code_img'   onclick='this.src=this.src+"&"+Math.random()' / alt="PHP 输出session 验证码与图片不同步,图片总是快一步,求解!" > 	<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);
登录后复制

PHP 输出session 验证码与图片不同步,图片总是快一步,求解!

当然

A.php输出

PHP 输出session 验证码与图片不同步,图片总是快一步,求解!
浏览器请求A.php,输出了SESSION

接着渲染A页面的时候,发现图片节点,接着请求captcha.php,服务器端接着产生验证图片,这时候又产生了新的SESSION值

看来还是得好好学习

是不是浏览器安装了调试插件的问题,使浏览器会加载页面两次

是不是浏览器安装了调试插件的问题,使浏览器会加载页面两次,所以验证码也加载了两次

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

在Laravel中使用Flash会话数据 在Laravel中使用Flash会话数据 Mar 12, 2025 pm 05:08 PM

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

php中的卷曲:如何在REST API中使用PHP卷曲扩展 php中的卷曲:如何在REST API中使用PHP卷曲扩展 Mar 14, 2025 am 11:42 AM

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

简化的HTTP响应在Laravel测试中模拟了 简化的HTTP响应在Laravel测试中模拟了 Mar 12, 2025 pm 05:09 PM

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

在Codecanyon上的12个最佳PHP聊天脚本 在Codecanyon上的12个最佳PHP聊天脚本 Mar 13, 2025 pm 12:08 PM

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

解释PHP中晚期静态结合的概念。 解释PHP中晚期静态结合的概念。 Mar 21, 2025 pm 01:33 PM

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸

自定义/扩展框架:如何添加自定义功能。 自定义/扩展框架:如何添加自定义功能。 Mar 28, 2025 pm 05:12 PM

本文讨论了将自定义功能添加到框架上,专注于理解体系结构,识别扩展点以及集成和调试的最佳实践。

如何用PHP的cURL库发送包含JSON数据的POST请求? 如何用PHP的cURL库发送包含JSON数据的POST请求? Apr 01, 2025 pm 03:12 PM

使用PHP的cURL库发送JSON数据在PHP开发中,经常需要与外部API进行交互,其中一种常见的方式是使用cURL库发送POST�...

See all articles