發(fā)表日期:2018-12 文章編輯:小燈 瀏覽次數(shù):3534
驗(yàn)證規(guī)則支持對(duì)表單的令牌驗(yàn)證,首先需要在你的表單里面增加下面隱藏域:
<input type="hidden" name="__token__" value="{$Request.token}" />
或者
{:token()}
然后在你的驗(yàn)證規(guī)則中,添加token
驗(yàn)證規(guī)則即可,例如,如果使用的是驗(yàn)證器的話(huà),可以改為:
protected $rule = ['name'=>'require|max:25|token','email' =>'email',];
如果你的令牌名稱(chēng)不是__token__
,則表單需要改為:
<input type="hidden" name="__hash__" value="{$Request.token.__hash__}" />
或者:
{:token('__hash__')}
驗(yàn)證器中需要改為:
protected $rule = ['name'=>'require|max:25|token:__hash__','email' =>'email',];
如果需要自定義令牌生成規(guī)則,可以調(diào)用Request
類(lèi)的token
方法,例如:
namespace app\index\controller;use think\Controller;class Index extends Controller{public function index(){$token = $this->request->token('__token__', 'sha1');$this->assign('token', $token);return $this->fetch();}}
然后在模板表單中使用:
<input type="hidden" name="__token__" value="{$token}" />
或者不需要在控制器寫(xiě)任何代碼,直接在模板中使用:
{:token('__token__', 'sha1')}