2017年05月12日 学习小总结(think php 验证码的使用)
使用版本think php 3.2.3
<!-- 模板代码 -->
<form action="__URL__/to_reg" method="post">
用户名:<input type="text" name="uresname"><br>
密 码:<input type="text" name="password"><br>
验证码:<input type="text" name="verify"><img src="__URL__/verify" onclick="this.src=this.src+'?'+Math.random()">
<input type="submit" name="" value="提交">
<input type="reset" name="" value="重置">
</form>
<!-- 说明:调用验证码的时候关键要使用img 和 src="" __URL__/verify 表示的是控制器里面的验证码方法 -->
<!-- 控制器部分 -->
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
$this->display();
}
public function verify() { //验证码方法
$Verify = new \Think\Verify();
$Verify->fontSize = 20; // 字体大小
$Verify->length = 4; // 验证码个数
$Verify->useNoise = false; // 是否有杂点
$Verify->imageH = 40; // 验证码高度
$Verify->imageW = 0; // 验证码高度
$Verify->entry();
}
public function to_reg(){
dump($_SESSION);
dump($_POST);
$code = $_POST['verify'];
dump($code);
//检测验证码是否正确 (截取手册上的方法)
$verify = new \Think\Verify();
$s=$verify->check($code, $id); //不需要MD5加密,也不需要区分大小写
dump($s);die();
if($s){
$this->success('验证码正确');
}else{
$this->error('验证码不正确');
}
}
}