大纲
Verify类的方法
参数 | 描述 |
---|---|
expire | 验证码的有效期(秒) |
useImgBg | 是否使用背景图片 默认为false |
fontSize | 验证码字体大小(像素) 默认为25 |
useCurve | 是否使用混淆曲线 默认为true |
useNoise | 是否添加杂点 默认为true |
imageW | 验证码宽度 设置为0为自动计算 |
imageH | 验证码高度 设置为0为自动计算 |
length | 验证码位数 |
fontttf | 指定验证码字体 默认为随机获取 |
useZh | 是否使用中文验证码 |
bg | 验证码背景颜色 rgb数组设置,例如 array(243, 251, 254) |
seKey | 验证码的加密密钥 |
codeSet | 验证码字符集合 3.2.1 新增 |
zhSet | 验证码字符集合(中文) 3.2.1 新增 |
Verify控制器的写法
<?php
namespace Home\Controller;
use Think\Controller;
use Think\Verify;
use Think\Session;
class VerifyController extends Controller
{
/**
* 生成验证码
*/
public function verify()
{
$this->display();
}
public function verify_c()
{
$verify = new Verify();
$verify->fontSize = 18;
$verify->length = 4;
$verify->useNoise = false;
//$verify->useZh = true; //设置中文 如果无法正常显示,请确认你的 ThinkPHP/Library/Think/Verify/zhttfs/ 目录下面存在中文字体文件。可以到c:/fonts下面copy一个中文字体放在<span style="font-family: Arial, Helvetica, sans-serif;">ThinkPHP/Library/Think/Verify/zhttfs/ 目录下面</span>
$verify->imageW = 130;
$verify->imageH = 50;
$verify->entry();
}
public function checkVerify()
{
echo $_POST["verify"];
$verify = new Verify();
$check = $verify->check($_POST["verify"]);
if(!$check)
{
$this->error("验证码错误");
}
else
{
$this->success("验证码正确");
}
}
}
验证码的实现
html:verify.html
<!DOCTYPE html>
<html>
<head>
<title>Verify</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script type="text/javascript">
/*
* 刷新验证码
*/
function freshVerify()
{
document.getElementById('verifyImg').src='__URL__/verify_c/'+Math.random();
}
</script>
<form action="__URL__/checkVerify" method="post">
<div>
<input type="text" name="verify" placeholder="验证码" />
<img title="刷新验证码" src="__URL__/verify_c" id="verifyImg" onClick='freshVerify()'/>
<input type="submit" value="submit" />
</div>
</form>
</body>
</html>
效果: