我在我的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
的不同值以及页面上已回显的内容。那是为什么?
答案 0 :(得分:1)
每次请求都会续订csrf
令牌。您在页面和数据库中看到不同的值,因为在页面呈现后数据库中的值已更新。
更具体地说,SQL Session处理程序替换了默认的php会话处理程序,这就是为什么在卸载方法https://github.com/bcosca/fatfree-core/blob/master/base.php#L1903内调用session_commit
的原因(在框架关闭时调用) down)将使用新值更新会话数据库表。
要想有办法重复使用单个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());
也许有一种更简单的方法,但我还没想到它。