[top-think/think][TP6.0 BUG]表单的令牌验证token_field()调用错误

2023-12-20 856 views
7

[0] Error in Request.php line 1972 Call to a member function set() on null

     * @param  string $name 令牌名称
     * @param  mixed  $type 令牌生成方法
     * @return string
     */
    public function buildToken(string $name = '__token__', $type = 'md5'): string
    {
        $type  = is_callable($type) ? $type : 'md5';
        $token = call_user_func($type, $this->server('REQUEST_TIME_FLOAT'));

        $this->session->set($name, $token);

        return $token;
    }

回答

2

Request类下session未初始化,修改如下

 public static function __make(App $app)
    {
        $request = new static();

        $request->server  = $_SERVER;
        $request->env     = $app->env;
        $request->get     = $_GET;
        $request->post    = $_POST ?: $request->getInputData($request->input);
        $request->put     = $request->getInputData($request->input);
        $request->request = $_REQUEST;
        $request->cookie  = $_COOKIE;
        $request->file    = $_FILES ?? [];
        // 初始化session对象
        $request->session =  $app->session;

        if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
            $header = $result;
        } else {
            $header = [];
            $server = $_SERVER;
            foreach ($server as $key => $val) {
                if (0 === strpos($key, 'HTTP_')) {
                    $key          = str_replace('_', '-', strtolower(substr($key, 5)));
                    $header[$key] = $val;
                }
            }
            if (isset($server['CONTENT_TYPE'])) {
                $header['content-type'] = $server['CONTENT_TYPE'];
            }
            if (isset($server['CONTENT_LENGTH'])) {
                $header['content-length'] = $server['CONTENT_LENGTH'];
            }
        }

        $request->header = array_change_key_case($header);

        return $request;
    }
9

你开启sessionInit中间件了么?

7

尴尬了,没注意看改动,第一版是默认开启的