自已写的验证图片类,与session接合作用时两个的值不相同,相关的文件有三个,如下:
文件1:validateimg.class.php 类文件 代码如下:
<?php
class ValidateImg
{
private $width;
private $height;
private $valinum;
private $img;
private $valicode;
function __construct($width=80,$height=30,$valinum=4)
{
$this->width=$width;
$this->height=$height;
$this->valinum=$valinum;
$this->valicode=$this->getValiCode();
}
function showImg()
{
//创建图像
$this->createImage();
//创建干扰点
$this->createDisturb();
//写入内容
$this->outPutText();
$this->outPutImage();
}
private function createImage()
{
$this->img=imagecreatetruecolor($this->width,$this->height);
$bgcolor=imagecolorallocate($this->img,rand(150,250),rand(150,250),rand(150,250));
imagefill($this->img, 0, 0, $bgcolor);
$bordercolor=imagecolorallocate($this->img,rand(0,150),rand(50,1250),rand(0,150));
imagerectangle($this->img,0,0,$this->width-1,$this->height-1,$bordercolor);
}
private function createDisturb()
{
for ($i=1;$i<floor($this->height*$this->width/20);$i++)
{
$color=imagecolorallocate($this->img, rand(150,250),rand(150,250),rand(150,250));
imagesetpixel($this->img, rand(1,$this->width-2), rand(1,$this->height-2), $color);
}
}
private function outPutText()
{
for ($i=0;$i<strlen($this->valicode);$i++)
{
$fwidth=floor($this->width/$this->valinum-($this->width/$this->valinum)/2)*($i+1);
$fontcolor=imagecolorallocate($this->img,rand(0,150),rand(0,150),rand(0,150));
imagechar($this->img, 6, $fwidth, rand(1,$this->height/2), $this->valicode[$i], $fontcolor);
}
}
private function outPutImage()
{
if (imagetypes()&IMG_GIF)
{
header("Content_Type:image/gif");
imagegif($this->img);
}else if (imagetypes() & IMG_JPG)
{
header("Content-Typer:image/jpeg");
imagejpeg($this->img);
}else if (imagetypes() & IMG_PNG)
{
header("Content_Type:image/png");
imagepng($this->img);
}else if (imagetypes() & IMG_WBMP)
{
header("Content-Type:image/vnd.wap.wbmp");
imagewbmp($this->img);
}
else{
die("对不起服务器暂时不支持,请联系网站管理员!");
}
}
private function getValiCode()
{
$strcode='ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
$code='';
for ($i=0;$i<$this->valinum;$i++)
{
$code.=$strcode[rand(0,strlen($strcode))];
}
return $code;
}
public function getSessionValicode()
{
return $this->valicode;
}
function __destruct(){
imagedestroy($this->image);
}
}
?>
文件2:valiimg.php 验证图片文件 文件代码如下:
<?php
session_start();
require 'includes/validateimg.class.php';
$valicode=new ValidateImg(100,30,4);
$valicode->showImg();
$_SESSION["validatecode"]=$valicode->getSessionValicode();文件3:index.php Session与验证图片上的内容输出文件 文件代码如下:
<?php
session_start();
echo $_SESSION["validatecode"];
?>
<img src="valiimg.php">代码到这里完了,下面是问题描述:
echo $_SESSION["validatecode"]; 这条语句输出的是上次验证图片上的验证码,请大伙帮我看一下,这是什么原因造成的。谢谢
文件1:validateimg.class.php 类文件 代码如下:
<?php
class ValidateImg
{
private $width;
private $height;
private $valinum;
private $img;
private $valicode;
function __construct($width=80,$height=30,$valinum=4)
{
$this->width=$width;
$this->height=$height;
$this->valinum=$valinum;
$this->valicode=$this->getValiCode();
}
function showImg()
{
//创建图像
$this->createImage();
//创建干扰点
$this->createDisturb();
//写入内容
$this->outPutText();
$this->outPutImage();
}
private function createImage()
{
$this->img=imagecreatetruecolor($this->width,$this->height);
$bgcolor=imagecolorallocate($this->img,rand(150,250),rand(150,250),rand(150,250));
imagefill($this->img, 0, 0, $bgcolor);
$bordercolor=imagecolorallocate($this->img,rand(0,150),rand(50,1250),rand(0,150));
imagerectangle($this->img,0,0,$this->width-1,$this->height-1,$bordercolor);
}
private function createDisturb()
{
for ($i=1;$i<floor($this->height*$this->width/20);$i++)
{
$color=imagecolorallocate($this->img, rand(150,250),rand(150,250),rand(150,250));
imagesetpixel($this->img, rand(1,$this->width-2), rand(1,$this->height-2), $color);
}
}
private function outPutText()
{
for ($i=0;$i<strlen($this->valicode);$i++)
{
$fwidth=floor($this->width/$this->valinum-($this->width/$this->valinum)/2)*($i+1);
$fontcolor=imagecolorallocate($this->img,rand(0,150),rand(0,150),rand(0,150));
imagechar($this->img, 6, $fwidth, rand(1,$this->height/2), $this->valicode[$i], $fontcolor);
}
}
private function outPutImage()
{
if (imagetypes()&IMG_GIF)
{
header("Content_Type:image/gif");
imagegif($this->img);
}else if (imagetypes() & IMG_JPG)
{
header("Content-Typer:image/jpeg");
imagejpeg($this->img);
}else if (imagetypes() & IMG_PNG)
{
header("Content_Type:image/png");
imagepng($this->img);
}else if (imagetypes() & IMG_WBMP)
{
header("Content-Type:image/vnd.wap.wbmp");
imagewbmp($this->img);
}
else{
die("对不起服务器暂时不支持,请联系网站管理员!");
}
}
private function getValiCode()
{
$strcode='ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
$code='';
for ($i=0;$i<$this->valinum;$i++)
{
$code.=$strcode[rand(0,strlen($strcode))];
}
return $code;
}
public function getSessionValicode()
{
return $this->valicode;
}
function __destruct(){
imagedestroy($this->image);
}
}
?>
文件2:valiimg.php 验证图片文件 文件代码如下:
<?php
session_start();
require 'includes/validateimg.class.php';
$valicode=new ValidateImg(100,30,4);
$valicode->showImg();
$_SESSION["validatecode"]=$valicode->getSessionValicode();文件3:index.php Session与验证图片上的内容输出文件 文件代码如下:
<?php
session_start();
echo $_SESSION["validatecode"];
?>
<img src="valiimg.php">代码到这里完了,下面是问题描述:
echo $_SESSION["validatecode"]; 这条语句输出的是上次验证图片上的验证码,请大伙帮我看一下,这是什么原因造成的。谢谢
session 的值必须与浏览器交互一下才有效