自已写的验证图片类,与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"]; 这条语句输出的是上次验证图片上的验证码,请大伙帮我看一下,这是什么原因造成的。谢谢
解决方案 »
- 前台如何实现与后台交互,以及如何快速建站!!!
- PHP脚本调用zip创建压缩包,如何仅添加相对目录,或者不添加目录信息
- 付费请人处理access数据库中的数据并导入到SupeSite7.0数据库中
- 问个几率问题
- //小弟自学php,对这段程序的注析不知道会不会有很多 知识错误。在这里肯请各位朋友指正。
- 50分~~谁有mysql5.0的windows下的安装包?上传一下~~
- 头脑
- 如何换行绘制一个超过显示宽度的长英文串?
- 这样怎么实现???
- 请大家帮帮忙,帮我解释一下eregi()中的规则符,我有些迷糊。谢谢
- 求高手解码 php 加密了
- 请问如何设置才能让我的apache+php网站只能通过我特定域名访问?
session 的值必须与浏览器交互一下才有效