thinphp验证码的简单实现
index.html
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>Document</title>
<script src=”__PUBLIC__/js/jquery-1.6.2.min.js”></script>
</script>
<script>
$(function(){
$(‘img’).click(function(){
$(‘img’).attr(‘src’,”__URL__/vcode/random/”+Math.random());
});
});
</script>
</head>
<body>
<form method=”post” action=”__URL__/check”>
用户名<input type=”text” name=”username”><br>
密码<input type=”password” name=”password”><br>
验证码<input type=”text” name=”code”><img src=”__URL__/vcode”><br>
<input type=”submit” name=””>
</form>
</body>
</html>
IndexController.class.php
<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
$this->display();
}
public function vcode(){
$Verify = new \Think\Verify();
$Verify->entry(1);
}
public function check(){
header(“Content-type: text/html; charset=utf-8”);
$code=I(‘code’);
$verify = new \Think\Verify();
if($verify->check($code, 1)){
echo “验证码正确”;
}else{
$this->success(“验证码错误”);
}
}
}