使用tp6驗證碼需要先引入
composer require topthink/think-captcha
自定義驗證碼 在項目模塊下新建控制器
<?php namespace app\admin\controller; use think\captcha\facade\Captcha; class Verify { public function index() { return Captcha::create(); } }
如果需要使用自己的配置,則在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' => [
'length'=>4,
'useCurve' => false,
//是否添加雜點
'useNoise' => false,
],
];
然后修改自定義驗證碼控制器方法為
return Captcha::create('verify');
前端使用
<div>
<img src="{:url('verify/index')}" onclick='this.src="{:url('verify/index')}?"+Math.random();' width="100" height="43" class="admin-captcha" alt="">
</div>
