Thinkphp3.2.3如何检验验证码?

用TP3.2框架在Verify控制器中生成验证码代码如下:

 public function verify_c { $Verify = new ThinkVerify; $Verify->fontSize = 18; $Verify->length = 4; $Verify->useNoise = false; $Verify->codeSet = 0123456789; $Verify->imageW = 130; $Verify->imageH = 50; //$Verify->expire = 600; $Verify->entry; }

在Verify控制器中输出验证码如下:

 public function pv { if !empty$_POST { $verify = new ThinkVerify; $code = $_POST[code]; if !empty$code { echo$code . "
";
// dump$code; if !empty$verify { echo$verify; var_dump$verify; } else { echo"验证码为空"; } } else { echo"未输入验证码"; } } }

前台代码如下:

 <form action="{:UVerify/pv,array}" method="post" id="captcha-container"> <input name="code" width="50%" height="50" class="captcha-text" placeholder="验证码" type="text" > <img width="130" class="left15" height="35" alt="验证码" src="{:UHome/Verify/verify_c,array}" title="点击刷新"> <input type="submit" value="提交"/> </form>

输入后提交,显示如下:

6436 objectThinkVerify[6] protected config => array size=15 seKey => string ThinkPHP.CN length=11 codeSet => string 2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY length=52 expire => int 1800 useZh => boolean false zhSet => string 们以我到他会作时要动国产的一是工就年阶义... length=4500 useImgBg => boolean false fontSize => int 25 useCurve => boolean true useNoise => boolean true imageH => int 0 imageW => int 0 length => int 5 fontttf => string length=0 bg => array size=3 0 => int 243 1 => int 251 2 => int 254 reset => boolean true private _image => null private _color => null

为什么取不到TP生成的验证码呢?

不用你取得代码,直接调用工具类中的check就可以验证了

$verify = new ThinkVerify;
return $verify->check$code;

自动完成中这样使用就更好了:

 /** * 验证验证码. * @param string $code 验证码. * @return bool */ protected function checkCode$code { $verify = new ThinkVerify; return $verify->check$code; }

ob_clean;//在初始化Verify前加上这个试试
$verify = new ThinkVerify;

 public function verify_c { ob_clean; $Verify = new ThinkVerify; $Verify->fontSize = 18; $Verify->length = 4; $Verify->useNoise = false; $Verify->codeSet = 0123456789; $Verify->imageW = 130; $Verify->imageH = 50; //$Verify->expire = 600; $Verify->entry; }

我自己解决了可以用以下来直接验证

 public function pv { if !empty$_POST { $verify = new ThinkVerify; $code = $_POST[code]; if!$verify->check$code { $this->error"验证码错误!"; return; }else{ echo "验证码正确!!";} } }

发表评论

电子邮件地址不会被公开。 必填项已用*标注