如何在 POST 请求期间刷新会话? [英] How can I refresh the session during a POST request?
问题描述
我正在 Symfony2 中构建一个用户登录的 Ajax 应用程序,然后从那时起,一切都由 POST 请求处理.在 config.yml
中将会话生存期定义为 5 分钟后,我遇到了用户会话在 5 分钟后失效的问题,即使他们发出 POST 请求.每次发出请求时,我都想在会话无效之前重置倒计时,但我不确定如何有效地做到这一点.
I'm building an Ajax app in Symfony2 where a user logs in, and then from that point forward everything is handled by POST requests. After defining the session lifetime to 5 minutes in config.yml
, I run into the problem of the user's session being invalidated after 5 minutes even though they're making POST requests. Each time a request is made, I'd like to reset the countdown before the session invalidates, but I'm not sure how to do this efficiently.
我目前考虑的方法是为kernel.request
事件编写一个监听器,检查请求方法是否为POST,并操作会话类.我还没有这样做,但这似乎不是一个干净的解决方案,因为每次发出请求时侦听器都需要触发.
The method I'm currently thinking about is writing a listener for the kernel.request
event, checking if the request method is POST, and manipulating the session class. I haven't done this yet, but it doesn't seem like a clean solution, since the listener needs to fire everytime a request is made.
这是我的会话配置:
session:
default_locale: %locale%
auto_start: true
lifetime: 300
有什么想法吗?解决方案是纯 PHP 而不是 Symfony?
Any ideas? Is the solution plain PHP and not Symfony?
推荐答案
会话的生命周期是它的最大年龄.这是通过一个已经创建的 cookie 控制的,并且不再从服务器端刷新(因为会话已经建立).您可以手动更新此 cookie,我认为这将与 symfony2 一起使用.
The lifetime of a session is it's maximum age. This is controlled via a cookie which has already been created and is not refreshed any longer from the server side (because the session is already established). You can just update this cookie manually and I think this will make it with symfony2.
最简单的方法可能是在不破坏会话的情况下重新生成会话 ID:
The probably easiest is to regenerate the session id w/o destroying the session:
$this->get('session')->migrate();
这应该会触发会话 cookie 的更新.
This should trigger an update on the session cookie.
可能相关的问题:
这篇关于如何在 POST 请求期间刷新会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!