PHP 输出session 验证码与图片不同步,图片总是快一步,求解! 本帖最后由 Xdawei 于 2012-04-18 20:07:42 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 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); <img id='code_img' onclick='this.src="getimg.php?Math.random()"' src='getimg.php'/> 当然A.php输出<img src="captcha.php" ……浏览器请求A.php,输出了SESSION接着渲染A页面的时候,发现图片节点,接着请求captcha.php,服务器端接着产生验证图片,这时候又产生了新的SESSION值 mysql的存储过程的参数没有生效 发表一下面试经验啊? 欢迎加入PHP zend framework群 想把自己做的网页放到网上,应该注意哪几方面问题? xml解析 同一个页面左右添加数据 请问对提交的数据不过滤 $ 有没有危险?? 如何禁止用户重复登录(急,在线等) apache 2.0好用还是1.0好用?php 4.3.2好用还是以前的好用?我被php的版本都搞乱套了。 再问正则 兄弟们,能帮我的PHP环境跑起来吗 【求指点】自学PHP,光盘赠送项目无法正常运行
这是什么意思?
你的代码中为什么要对$_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
<?php
session_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<?php
session_start();
var_dump($_SESSION);
echo "<HR/>";
var_dump($_REQUEST);