SW-X内置了一个图形验证码类,基于\x\verify\Figure
类做驱动支持。
正常情况下控制器继承\x\controller\Http
基类后,我们可以直接使用verify()
方法生成验证码,verify_check()
方法校验验证码。
下面,我们先来看下/config/app.php
中,有关验证码的相关配置信息:
<?php
// +-----------------------------
// | 验证码设置
// +-----------------------------
'verify' => [
// 验证码字体大小(px)
'fontsize' => 20,
// 验证码图片高度
'height' => 32,
// 验证码图片宽度
'width' => 150,
// 验证码位数
'length' => 4,
// 验证码字体样式
'ttf' => '6.ttf',
// 验证码过期时间,单位:秒
'expire' => 60,
// 是否添加混淆曲线
'curve' => true,
// 是否添加杂点
'noise' => true,
// 发起验证后是否需要更新验证码
'update' => true,
],
依赖类:\x\controller\Http
方法名:verify()
参数:
int $type
1
1
英数混合,2
数字运算string $session_name
__vif__
array $config
[]
,非必填
返回值:无
示例控制器代码:
namespace app\http;
use x\controller\Http;
/**
* @Controller(prefix="")
*/
class Index extends Http
{
/**
* @RequestMapping(route="/", method="get", title="主页")
*/
public function index() {
// 生成验证码
return $this->verify();
}
}
依赖类:\x\controller\Http
方法名:verify_check()
参数:
string $code
string $session_name
__vif__
返回值:bool
示例控制器代码:
namespace app\http;
use x\controller\Http;
/**
* @Controller(prefix="")
*/
class Index extends Http
{
/**
* @RequestMapping(route="/", method="get", title="主页")
*/
public function index() {
$param = \x\Request::param();
// 校验验证码
$bool = $this->verify_check($param['code']);
return $this->fetch(dd($bool));
}
}