开发者社区> 问答> 正文

验证码图片与SESSION中的值不同步:报错 

验证码图片与SESSION中的值不同步?
不知什么原因,求解决
实例:http://wwsw.janwan.info
index.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" /> <title>wwswERP</title> <script src="js/jquery-1.7.1.min.js" type="text/javascript"></script> <link href="css/main.css" rel="stylesheet" type="text/css" /> <script> function usercheck(){ if ($("#username").val()==""){ $("#sName").html('<font style=font-size:5px face=微软雅黑 color=red>*用户名不能为空</font>'); } else{ //$("#sName").html('用户名输入正确'); $.get("lib/check_regdata.php?ajax_a=0&tttt="+Math.random()+"&ajax_user="+$("#username").val(), function(result){ $("#sName").html(result); });

} }

function mailcheck(){ if ($("#email").val()==""){ $("#sMail").html('<font style=font-size:5px face=微软雅黑 color=red>*邮箱名不能为空</font>'); } else{ //$("#sName").html('用户名输入正确'); $.get("lib/check_regdata.php?ajax_a=1&tttt="+Math.random()+"&ajax_email="+$("#email").val(), function(result){ $("#sMail").html(result); });

} }

function pwdcheck(){ if ($("#password_1").val()==""){ $("#sPwd").html('<font style=font-size:5px face=微软雅黑 color=red>*密码不能为空</font>'); } else{ $("#sPwd").html('<font style=font-size:5px face=微软雅黑 color=red>*密码输入正确</font>'); } }

function pwdsamecheck(){ if ($("#password_1").val()!=""){ if ($("#password_1").val()==$("#password_2").val()){ $("#srPwd").html('<font style=font-size:5px face=微软雅黑 color=red>*密码一致</font>'); } else{ $("#srPwd").html('<font style=font-size:5px face=微软雅黑 color=red>*密码不一致</font>'); } } else{ $("#sPwd").html('<font style=font-size:5px face=微软雅黑 color=red>*请填写密码</font>'); $("#srPwd").html('<font style=font-size:5px face=微软雅黑 color=red>*密码不一致</font>'); } }

function Ninacheck(){ if ($("#nickname").val()==""){ $("#snName").html('<font style=font-size:5px face=微软雅黑 color=red>昵称不能为空</font>'); } else{ $("#snName").html('<font style=font-size:5px face=微软雅黑 color=red>昵称输入正确</font>'); } }

function Trnacheck(){ if ($("#truename").val()==""){ $("#stName").html('<font style=font-size:5px face=微软雅黑 color=red>姓名不能为空,必须为真实姓名</font>'); } else{ $("#stName").html('<font style=font-size:5px face=微软雅黑 color=red>姓名输入正确,必须为真实姓名</font>'); } }

function codecheck(){ if ($("#keycode").val()==""){ $("#scode").html('<font style=font-size:5px face=微软雅黑 color=red>*验证码不能为空</font>'); } else{ $.get("lib/check_regdata.php?ajax_a=2&tttt="+Math.random()+"&ajax_code="+$("#keycode").val(), function(result){ $("#scode").html(result); });

} }

</script> </head> <body id="html_all"> <?php for($s=0;$s<5;$s++) echo "<br>"; ?> <table border="0" background=img/login_bg.jpg style="width:600px;height:300px;" align="center"> <tr> <td> <form action="check_user.php" method="POST" align="center"> <table align="center" border="0"> <tr> <td><font style=font:15px face=微软雅黑 color=#009393><b>帐   号:</b></font></td> <td><input name="a_username" type="text" style="width:150px"></td></tr><tr> <td><font style=font:15px face=微软雅黑 color=#009393><b>密   码:</b></font></td> <td><input name="password" type="password" style="width:150px"></td><td  rowspan="2"><img src="lib/checkcode.php?key_class=1" width="100px" height="50px" onclick="this.src='lib/checkcode.php?key_class=1&'+Math.random();"></td></tr><tr> <td><font style=font:15px face=微软雅黑 color=#009393><b>验证码:</b></font></td> <td><input name="keycode" type="text" style="width:150px"></td></tr><tr> <td></td> <td><input name="login"type="image" src="img/login.jpg" value="登陆"></td> <tr></tr> <tr></tr> <td></td> <td></td> <td><a href = "javascript:void(0)" onclick = "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'"><font color=#009900>注册</font></a>  <a href = "javascript:void(0)" onclick = "document.getElementById('light2').style.display='block';document.getElementById('fade2').style.display='block'"><font color=#009900>忘记密码?</font></a></td> </tr> </table> </form> </td> </tr> </table> <?php for($s=0;$s<6;$s++) echo "<br>"; ?> <div id="light" class="white_content"><div align="right"><a href = "javascript:void(0)" onclick = "document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'">关闭</a></div><br> <form action="lib/lib.php?reg_user=ture" method="POST" align="center" id="reg"> <div align="center" ><font style=font:15px face=微软雅黑 color=#009393><b>万维思维系统注册</b></font></div><br> <table align="center" border="0"> <tr> <td width="100"><font style=font:15px face=微软雅黑 color=#009393><b>帐   号:</b></font></td> <td><input name="username" type="text" style="width:150px" id="username" onblur="javascript:usercheck();" /></td><td width="250"><span id="sName"></span></td></tr><tr>    <td width="100"><font style=font:15px face=微软雅黑 color=#009393><b>邮   箱:</b></font></td> <td><input name="email" type="text" style="width:150px" id="email" onblur="javascript:mailcheck();" /></td><td width="250"><span id="sMail"></span></td></tr><tr> <td width="100"><font style=font:15px face=微软雅黑 color=#009393><b>密   码:</b></font></td> <td><input name="password_1" type="password" style="width:150px" id="password_1" onblur="javascript:pwdcheck();"></td><td width="250"><span id="sPwd"></span></td></tr><tr> <td width="100"><font style=font:15px face=微软雅黑 color=#009393><b>重   复:</b></font></td> <td><input name="password_2" type="password" style="width:150px" id="password_2" onblur="javascript:pwdsamecheck();"></td><td width="250"><span id="srPwd"></span></td></tr><tr> <td width="10"><font style=font:15px face=微软雅黑 color=#009393><b>昵   称:</b></font></td> <td><input name="nickname" type="text" style="width:150px" id="nickname" onblur="javascript:Ninacheck();"></td><td width="250"><span id="snName"></span></td></tr><tr> <td width="100"><font style=font:15px face=微软雅黑 color=#009393><b>姓   名:</b></font></td> <td ><input name="truename" type="text" style="width:150px" id="truename" onblur="javascript:Trnacheck();"></td><td width="250"><span id="stName"></span></td></tr><tr> <td width="100"><font style=font:15px face=微软雅黑 color=#009393><b>验证码:</b></font></td>         <td><input name="keycode" type="text" style="width:150px" id="keycode" onblur="javascript:codecheck();"></td><td width="250"><span id="scode"></span></td></tr><tr> <td></td><td  rowspan="2" align="left"><img src="lib/checkcode.php?key_class=2" width="100px" height="50px" onclick="this.src='lib/checkcode.php?key_class=2&'+Math.random();"></td></tr><tr></tr><tr> <td></td>         <td align=center><br><input name="reg_user" type="image" src="img/reg.gif" value="注册" ></td></tr> </table> </form> </div>         <div id="fade" class="black_overlay"></div> <div id="light2" class="white_content"><div align="right"><a href = "javascript:void(0)" onclick = "document.getElementById('light2').style.display='none';document.getElementById('fade2').style.display='none'">关闭</a></div><br> <form action="lib/lib.php?rec_pass=ture" method="POST" align="center"> <div align="center" ><font style=font:15px face=微软雅黑 color=#009393><b>万维思维系统密码恢复</b></font></div><br> <table align="center" border="0"> <tr> <td><font style=font:15px face=微软雅黑 color=#009393><b>帐   号:</b></font></td> <td><input name="username" type="text" style="width:150px"></td></tr><tr> <td><font style=font:15px face=微软雅黑 color=#009393><b>邮   箱:</b></font></td> <td><input name="usermail" type="text" style="width:150px"></td><td  rowspan="2"><img src="lib/checkcode.php?key_class=3" width="100px" height="50px" onclick="this.src='lib/checkcode.php?key_class=3&'+Math.random();"></td></tr><tr> <td><font style=font:15px face=微软雅黑 color=#009393><b>验证码:</b></font></td>         <td><input name="keycode" type="text" style="width:150px"></td> </tr><tr> <td></td>         <td><br><input name="reg"type="image" src="img/REC_PASS.JPG" value="密码恢复"></td></tr> </table> </form> </div>         <div id="fade2" class="black_overlay"></div>

</body> </html>


check_data


<?php //header("Content-type: text/html; charset=utf-8");   include "../config.php"; include "lib.php"; $ajax_a=$_GET["ajax_a"]; switch($ajax_a){ case 0: $ajax_user=$_GET["ajax_user"]; if(!isset($ajax_user)){$ajax_user=1;} $send_commond=mysql_query("select * from wwsw_user where username='$ajax_user'"); if (mysql_num_rows($send_commond)>0){ echo "<font style=font-size:5px face=微软雅黑 color=red>*此用户名不可用</font>"; } else{ echo "<font style=font-size:5px face=微软雅黑 color=red>*此用户名可用</font>"; } break; case 1: $ajax_email=$_GET["ajax_email"]; if(!isset($ajax_email)){$ajax_email=1;} if (preg_match("^([a-za-z0-9_-])+@([a-za-z0-9_-])+(.[a-za-z0-9_-])+^", $ajax_email)){  $send_commond=mysql_query("select * from wwsw_user where email='$ajax_email'"); if (mysql_num_rows($send_commond)>0){ echo "<font style=font-size:5px face=微软雅黑 color=red>*此邮箱名不可用</font>"; } else{ echo "<font style=font-size:5px face=微软雅黑 color=red>*此邮箱名可用</font>"; } }  else{  echo "<font style=font-size:5px face=微软雅黑 color=red>*邮箱格式不正确</font>";     } 

break; case 2: Session_start(); $ajax_code=$_GET["ajax_code"]; $key=$_SESSION["sReg"]; if(!isset($ajax_code)){$ajax_code=2;} if($ajax_code==$_SESSION["sReg"]){ echo "<font style=font-size:5px face=微软雅黑 color=red>*验证码正确</font>"; echo $key; //unset($_SESSION["sReg"]); } else{ echo "<font style=font-size:5px face=微软雅黑 color=red>*验证码不正确</font>"; echo $key; //unset($_SESSION["sReg"]); } break; } ?>


check_code    此代码来自OSCHINA


<?php // +------------------------------------------------------------------------ // 验证码类,该类的对象能动态获取验证码图片,验证码字符保存在SESSION['code']中  // +------------------------------------------------------------------------ // 支持4种格式 数字 字母 汉字 混合 // +------------------------------------------------------------------------ // @Author : HelloChina(sanzi0930@163.com)                            // +------------------------------------------------------------------------ // @Date: 2012年6月7日11:03:00      // +------------------------------------------------------------------------ // @version 1.0                                                      // +------------------------------------------------------------------------

class Vcode{ protected $width; //验证码宽度 protected $height; //验证码长度 protected $codeNum; //验证码字符个数 protected $codeType; //验证码类型 protected $fontSize; //字符大小 protected $fontType; //字体类型 protected $codeStr; //中文内容 protected $strNum; //中文个数 protected $imageType; //输出图片类型 protected $image; //图片资源 protected $checkCode; //验证码内容      /**       +--------------------------------------------------------------------------------       * 取得验证码信息       +--------------------------------------------------------------------------------       * @param integer $width 验证码宽度        * @param integer $height 验证码高度        * @param integer $codeNum 验证码字符个数
      * @param integer $codeType 验证码字符类型 1为数字 2为字母 3为汉字 4为混编       * @param integer $fontSize        验证码字体的大小       * @param string  $fontType 验证码字体类型       * @param string  $imageType 验证码输出图片类型       * @param string  $codestr 中文验证码内容       +--------------------------------------------------------------------------------      */ public function __construct($width=100, $height=50, $codeNum=4, $codeType=4, $fontSize=12, $fontType='heiti.ttf' ,$imageType='jpeg', $codeStr='去我饿人他一哦平啊是的飞个好就看了在想才吧你吗'){ $this->width =  $width; $this->height =  $height; $this->codeNum =  $codeNum; $this->codeType =  $codeType; $this->fontSize  =  $fontSize; $this->fontType =  $fontType; $this->codeStr =  $codeStr; $this->strNum = strlen($this->codeStr)/3-1; $this->imageType =  $imageType; $this->checkCode =  $this->getCheckCode(); }

      //+--------------------------------------------------------------------------------       //* 生成验证码字符       //+--------------------------------------------------------------------------------       //* @return string       //+-------------------------------------------------------------------------------- public function __toString(){
$string = implode('', $this->getCheckCode()); $key_class=$_GET["key_class"]; session_start(); if($key_class==1){$_SESSION["sLogin"]=$string;} if($key_class==2){$_SESSION["sReg"]=$string;} if($key_class==3){$_SESSION["sRec"]=$string;} // $_SESSION["sLogin"]=$string;  //登陆session中 $this->getImage();              //输出验证码 return ''; }
protected function getCheckCode(){ $string = array();
switch($this->codeType){ case 1: //数字字符串 $string =  array_rand(range(0,9), $this->codeNum); break; case 2: //大字母字符串 $string = array_rand(array_flip(range('A', 'Z')), $this->codeNum); break; case 3: //汉字字符串

for($i=0; $i<($this->codeNum); $i++){ $start = mt_rand(0, $this->strNum); $string[$i]= self::msubstr($this->codeStr,$start); } break; case 4: //混合字符串 for($i=0; $i<($this->codeNum); $i++){ $rand=mt_rand(0,2); switch($rand){ case 0: $ascii = mt_rand(48,57); $string[$i] = sprintf('%c',$ascii); break; case 1: $ascii = mt_rand(97,122); $string[$i] = sprintf('%c',$ascii); break; case 2: $start = mt_rand(0, $this->strNum); $string[$i] = self::msubstr($this->codeStr,$start); break; }

}

} return $string; }       //+--------------------------------------------------------------------------------       //* 中文截取 ThinkPHP中的中文截取checkCode       //+--------------------------------------------------------------------------------       //* @return string       //+-------------------------------------------------------------------------------- static protected function msubstr($str, $start=0, $length=1, $charset="utf-8") {         if(function_exists("mb_substr"))             $slice = mb_substr($str, $start, $length, $charset);         elseif(function_exists('iconv_substr')) {             $slice = iconv_substr($str,$start,$length,$charset);         }else{             $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";             $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";             $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";             $re['big5']   = "/[\x01-\x7f]|\x81-\xfe/";             preg_match_all($re[$charset], $str, $match);             $slice = join("",array_slice($match[0], $start, $length));         }         return $slice;     }       //+--------------------------------------------------------------------------------       //* 分配画布资源        //+--------------------------------------------------------------------------------       protected function createRes(){ $this->image = imagecreatetruecolor($this->width,$this->height); }       //+--------------------------------------------------------------------------------       //* 填充背景颜色        //+--------------------------------------------------------------sanzi0930@163.com------------------

protected function bgColor(){ $write = imagecolorallocate($this->image, 255, 255, 255); imagefill($this->image, 0, 0, $write); }       //+--------------------------------------------------------------------------------       //* 背景线条       //+-------------------------------------------------------------------------------- protected function filledLine(){ //横线

$lineColor1 = imagecolorallocate($this->image, 0xda, 0xd9, 0xd1);         for($j=3; $j<=$this->height-3; $j=$j+3){         imageline($this->image, 2, $j, $this->width-2, $j, $lineColor1);     } //竖线 $lineColor2 = imagecolorallocate($this->image, 0xda,0xd9,0xd1);     for($j=2;$j<100;$j=$j+6){        imageline($this->image, $j, 0, $j+8, $this->height, $lineColor2); } }

      //+--------------------------------------------------------------------------------       //* 画边框       //+-------------------------------------------------------------------------------- protected function bgBorder(){ $bordercolor = imagecolorallocate($this->image, 0x9d, 0x9e, 0x96);         imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $bordercolor); }

      //+--------------------------------------------------------------------------------       //* 字符颜色 $this->getCheckCode()       //+-------------------------------------------------------------------------------- protected function textColor(){ $fontColor[]  = imagecolorallocate($this->image, 0x15, 0x15, 0x15);     $fontColor[]  = imagecolorallocate($this->image, 0x95, 0x1e, 0x04);     $fontColor[]  = imagecolorallocate($this->image, 0x93, 0x14, 0xa9);     $fontColor[]  = imagecolorallocate($this->image, 0x12, 0x81, 0x0a);     $fontColor[]  = imagecolorallocate($this->image, 0x06, 0x3a, 0xd5);     return $fontColor; }

      //+--------------------------------------------------------------------------------       //* 把字符写到画布上去       //+--------------------------------------------------------------------------------

protected function writeText(){ $fontColor = $this->textColor(); $rndstring = $this->getCheckCode();

for($i=0; $i<4; $i++){         $c = mt_rand(-30, 30); $x=floor($this->width/$this->codeNum)$i+5; $y = $this->height/2+$this->fontSize/2; imagettftext($this->image, $this->fontSize, $c, $x, $y, $fontColor[$i], $this->fontType, $rndstring[$i]); } }       //+--------------------------------------------------------------------------------       // 输出图片格式       //+-------------------------------------------------------------------------------- protected function output(){ $func='image'.$this->imageType; $header='Content-type:image/'.$this->imageType; if(function_exists($func)){ header($header); $func($this->image); }else{ echo '本系统不支持此方法'; return false; } }       //+--------------------------------------------------------------------------------       //* 输出验证码       //+-------------------------------------------------------------------------------- protected function getImage(){ $this->createRes(); $this->bgColor(); $this->bgBorder(); $this->filledline(); $this->writeText(); $this->output(); }       //+--------------------------------------------------------------------------------       //* 销毁图片       //+-------------------------------------------------------------------------------- public function __destruct(){  imagedestroy($this->image); }

}

$code = new Vcode(); echo $code;

?>

展开
收起
kun坤 2020-06-04 21:16:03 570 0
1 条回答
写回答
取消 提交回答
  • 嚓类,这代码,疼######你觉得有人会看这一屏没有格式的代码吗?###### 没有看你的一点代码。
    说说我以前碰到的问题
    有的验证码题就是 当前页面产生的验证码图片 和当前页面session获取验证码的值不相等。
    需要转入后台处理的 从session中获取验证码的值 才相等 ######回复 @命运的陶醉 : 我说的是java中验证码的一些问题######还不是很明白,能不能详细点,我现在是用到验证码时我才加载验证码,这样才行,你能不能具体说下你的方法?

    2020-06-08 09:47:41
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
面向失败设计 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载