使用tp6的表单令牌进行验证,结果报错了?

浏览:1611 发布日期:2020/04/25 分类:求助交流
<form action="/wap/index/index" method="post">
        姓名:<input type="text" name="username" value=""/><br/>
        手机:<input type="text" name="mobile" value=""/><br/>
       
        <!-- 表单令牌 -->
        <input type="hidden" name="__token__" value="{$token}" />

        <input type="submit" name="submit" value="提交">
    </form>
<?php
namespace app\wap\controller;

use app\BaseController;
use think\Request;
use think\facade\View;              // 门面方法
use think\exception\ValidateException;  // 异常

    public function index(Request $request)
    {
        // 表单令牌
        $token = $request->buildToken('__token__', 'sha1');

        if(request()->isPost()){
            // 令牌验证
            $check = $request->checkToken('__token__',  $request->param());
            if(false === $check) {
                throw new ValidateException('令牌无效');
            }
          exit;
        }


        // 门面方法
        View::assign('name', 'ThinkPHP');
        View::assign('token', $token);

        return view();
    }

为何会报错?错在哪?
还有 这个 $token ,我获取为空!!!


但是模板确实有值,只不过没有获取到?

1、$check = $request->checkToken('__token__'); 为何这行获取不到值!!!

2、throw new ValidateException('令牌无效'); 为何这行会报错?
最佳答案
评论() 相关
后面还有条评论,点击查看>>