1. 程式人生 > >thinkphp5 自定義驗證碼使用

thinkphp5 自定義驗證碼使用

例如 function 標識 添加 log 功能 內置 用戶 size

控制器【https://blog.csdn.net/John_rush/article/details/80169702】

public function verify(){
$captcha = new \think\captcha\Captcha();
//使用中文驗證碼
$captcha->useZh = true;
//驗證碼過期時間(s)
$captcha->expire =1800;
//是否畫混淆曲線
$captcha->useCurve =true;
//是否添加雜點
$captcha->useNoise = true;


//驗證碼位數
$captcha->length = 4;
//驗證成功後是否重置
$captcha->reset = true;
// 設置驗證碼字符
$captcha->zhSet = ‘們以我到他會作時要動國產的一是工就年階義發成部民可出能方進在了不和有大這‘;
return $captcha->entry();
}
頁面調用
<img src="{:url(‘common/verify‘)}" alt="captcha" onclick="this.src=‘{:url(‘common/verify‘)}?seed=‘+Math.random()" height="42" id="captcha" />

點擊刷新
onclick="this.src=‘{:url(‘common/verify‘)}?seed=‘+Math.random()"
驗證
//使用TP5的內置驗證功能,添加captcha驗證規則即可

$this->validate($data,[
‘captcha|驗證碼‘=>‘require|captcha‘
]);

//或者手動驗證

if(!captcha_check($captcha)){
//驗證失敗
};

//用Captcha類的check方法檢測驗證碼的輸入是否正確,例如,下面是封裝的一個驗證碼檢測的函數:

// 檢測輸入的驗證碼是否正確,$code為用戶輸入的驗證碼字符串,$id多個驗證碼標識


function check_verify($code, $id = ‘‘){
$captcha = new Captcha();
return $captcha->check($code, $id);
}


---------------------
作者:opfano_o
來源:CSDN
原文:https://blog.csdn.net/qazx123q/article/details/79724682

thinkphp5 自定義驗證碼使用