**TP6验证码自定义用法瞎搞哒**
1.安装扩张包
根目录执行命令: composer require topthink/think-captcha
2.打开/config/captcha.php文件 自定义验证规则
<?php
// +----------------------------------------------------------------------
// | Captcha配置文件
// +----------------------------------------------------------------------
return [
//验证码位数
'length' => 5,
// 验证码字符集合
'codeSet' => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',
// 验证码过期时间
'expire' => 1800,
// 是否使用中文验证码
'useZh' => false,
// 是否使用算术验证码
'math' => false,
// 是否使用背景图
'useImgBg' => false,
//验证码字符大小
'fontSize' => 25,
// 是否使用混淆曲线
'useCurve' => true,
//是否添加杂点
'useNoise' => true,
// 验证码字体 不设置则随机
'fontttf' => '',
//背景颜色
'bg' => [243, 251, 254],
// 验证码图片高度
'imageH' => 0,
// 验证码图片宽度
'imageW' => 0,
// 添加自定义的验证码设置
'verify' => [
'codeSet' => '2345678019',
'expire' => 18000, // 验证码过期时间(s)
'useImgBg' => false, // 使用背景图片
'fontSize' => 18, // 验证码字体大小(px)
'useCurve' => false, // 是否画混淆曲线
'useNoise' => false, // 是否添加杂点
'useZh' => false, // 使用中文验证码
'length' => 4, // 验证码位数
'fontttf' => '', // 验证码字体,不设置随机获取
'bg' => array(243, 251, 254), // 背景颜色
'reset' => true, // 验证成功后是否重置
],
];
3.打开/app/middleware.php 将session打开
// Session初始化
\think\middleware\SessionInit::class
4.前端展示
<input class="input" type="text" maxlength="4" ref="code" v-model="code" placeholder="输入你的验证码 @input="codeInp" @keyup.enter="checkForm">
<span class="xian"></span>
<img class="newcode" id="newcode" src="{:captcha_src('verify')}" alt="captcha" οnclick="this.src=this.src+'?'" style="height: 140%;float: right"/>
4.后台验证
if(!captcha_check($content['code'])){
$res['status'] = 0;
$res['msg'] = '请输入正确验证码';
return json($res);
}