tp5下直接生成的驗(yàn)證碼, 有些時(shí)候會(huì)不符合我們的要求, 這時(shí)需要用到自定義代碼的生成, 然而TP5文檔手冊(cè)寫(xiě)的實(shí)例不是非常清晰, 很多人看了容易云里霧里, 這里分享給大家 驗(yàn)證碼的生成,調(diào)用,及驗(yàn)證方法
1. 引入Captcha及生成自定義函數(shù)代碼
namespace app\index\controller;
use think\Controller;
use think\Config;
use think\Validate;
use think\captcha\Captcha;
/*use think\Request;*/
class Stu extends Controller{
public function getCode(){
$config = [
// 驗(yàn)證碼字體大小
'fontSize' => 30,
// 驗(yàn)證碼位數(shù)
'length' => 3,
// 關(guān)閉驗(yàn)證碼雜點(diǎn)
'useNoise' => false,
'useZh' => true
];
$captcha = new Captcha($config);
return $captcha->entry();
}
}
2. 視圖文件引用
<img src="<?php echo url("stu/getcode"); ?>"
onclick="this.src='<?php echo url("stu/getcode"); ?>?id='+Math.random();" />
3. 使用TP 驗(yàn)證碼驗(yàn)證
$validate = new Validate([
'__token__' => 'require|token'
]);
$data = [
'captcha' => $this->request->param("code")
];
if (!$validate->check($data)) {
echo($validate->getError());
}
