fatfree 会话,数据库中的不同值和 echo stmt

fatfree sessions, different values in database and echo stmt

我的 beforeroute() 控制器中有这个

    public function beforeroute()
{
    new \DB\SQL\Session($this->db);

    $mapper = new \DB\SQL\Mapper($this->db, 'users');

    $auth = new \Auth($mapper, array(
        'id' => 'username',
        'pw' => 'password'
    ));

    if (!$auth->login('validuser', '1234')) {
        die('username or password wrong');
    } else {
        echo ($csrf = $this->db->exec('SELECT csrf FROM sessions')[0]['csrf']);
    }
}

点击页面后,数据库中的 csrf 值和页面上的回显值不同。这是为什么?

csrf 令牌在每次请求时都会更新。您在页面和数据库中看到不同的值,因为数据库中的值在页面呈现后更新。

更具体地说,SQL 会话处理程序替换了默认的 php 会话处理程序,这就是为什么在卸载方法 https://github.com/bcosca/fatfree-core/blob/master/base.php#L1903 中调用 session_commit(在框架关闭时调用)将使用新值更新您的会话数据库table。

要有办法为您的目的重用单个 csrf 令牌,只需将其放回会话本身即可:

$s = new \DB\SQL\Session($f3->get('DB'));
// old value from last request
echo $f3->get('SESSION.csrf');
// remember current value for next request
$f3->set('SESSION.csrf',$s->csrf());

也许有更简单的方法,但我还没有想出来。