上代码:
login.html
<!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" />
<title>管理员登录</title>
</head>
<body>
<div class="logindiv">
<form action="http://localhost/ICxinyong/admin/index.php?c=Login" method="post" id="loginform">
<ul>
     <li class="row"><div class="firstcol">用户名:</div><div class="secondcol"><input type="text" id="username" name="username"/></div></li>
        <li class="row"><div class="firstcol">密&nbsp;&nbsp;码:</div><div class="secondcol"><input type="password" id="password" name="password" /></div></li>
        <li class="row"><div class="firstcol">验证码:</div><div class="secondcol"><input type="text" id="authnum"  maxlength="4" name="authnum" /><img  src="http://localhost/ICxinyong/admin/model/mod_imgAuth.php" id="authnumimg" title="看不清?点击换张图片!" onclick="this.src='http://localhost/ICxinyong/admin/model/mod_imgAuth.php?a='+Math.random()"/></div></li>
        <li class="row"><div class="firstcol"><input name="submit" type="submit" value="登录" id="submit" /></div><div class="secondcol"><input name="reset" type="reset" value="重置" id="reset" /></div></li>
    </ul>
</form>
</div>
</body>
</html>
生成图片验证码的mod_imgAuth.php
class imgAuth{
protected $im;
protected $color;
protected $auth;
protected $bgcolor;
protected $stringcolor;
protected $stringX=10;
protected $stringY=3;
protected $fontFamily=5;
protected $authLength=4;
/**
 * @return the $auth
 */
public function getAuth() {
return $this->auth;
} public function __construct($imwidth,$imheight){
$this->im=imagecreate($imwidth,$imheight) or die("Cant's initialize new GD image stream!");
$this->bgcolor=$this->setColor(rand(170,255),rand(170,255),rand(170,255));
$this->stringcolor=$this->setColor(rand(0,70),rand(0,70),rand(0,70));
$this->color=$this->setColor(rand(170,255),rand(170,255),rand(170,255));
$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
$list=explode(",",$ychar);
$a="";
for($i=0;$i<$this->authLength;$i++){
   $randnum=rand(0,count($list));
   $a.=$list[$randnum]." ";
}
session_start();
$_SESSION["authnum"]=$a;
$this->auth=$a;
}
public function setColor($r,$g,$b){
return imageColorAllocate($this->im,$r,$g,$b);
}
public function randomChar(){
//ed ����һ��ո�
$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
$list=explode(",",$ychar);
for($i=0;$i<$this->authLength;$i++){
   $randnum=rand(0,count($list));
   $this->auth.=$list[$randnum]." ";
}
}
public function fill($x,$y){
imagefill($this->im,$x,$y,$this->bgcolor);
}
public function imageString(){
imagestring($this->im,$this->fontFamily,$this->stringX,$this->stringY,$this->auth,$this->stringcolor);
}
public function  addpixel($x,$y){
imagesetpixel($this->im, $x , $y , $this->color ); 
}
public function draw($imwidth,$imheight){
$this->fill(0,0);
//$this->randomChar();
$this->imageString();
for($i=0;$i<400;$i++){
$x=rand(0,$imwidth);
$y=rand(0,$imheight);
$this->addpixel($x,$y);
}
}
public function outputImg(){
imagePNG($this->im); 
imageDestroy($this->im);  
}

}
$ia=new imgAuth(90,20);
$ia->draw(90,20); 
header("Content-type: image/PNG");
$ia->outputImg();验证图片验证码的index.php
session_start();
$a=$_SESSION["authnum"];
$authnum=$_post["authnum"];
if(strtolower($authnum)==strtolower($a)){
    echo "验证码相同";
}else{
    echo "验证码不同";
}
不知道为什么session取到的验证码和login.html显示的总是不同
请大侠指教

解决方案 »

  1.   

    $authnum=$_POST["authnum"];
    要大写
      

  2.   

    代码太乱太多了!
    自己想办法调试一下后台里的session,
      

  3.   

    PHP 图片验证码的怪问题session不同步只是所有传过去的session值都不同步么?
    那么index.php获取到的session值是什么?是否有重复赋值。。
      

  4.   

    注意这段
    $a="";
    for($i=0; $i<$this->authLength; $i++) {
      $randnum=rand(0,count($list));
      $a.=$list[$randnum]." ";
    }
    session_start();
    $_SESSION["authnum"]=$a;
    $this->auth=$a;
    $a被用于$_SESSION["authnum"],但它的格式是"A B C D"样式,每个字符中都有一个空格;而$_POST['authnum']是"ABCD"样式,没有空格,当然session取到的验证码和login.html显示的总是不同..