我使用以下代码在symfony2中使会话到期:
public function onKernelRequest(GetResponseEvent $event)
{
$this->maxIdleTime = ($this->container->getParameter('session_max_idle_time') > 0) ? $this->container->getParameter('session_max_idle_time') : 1200;
if (HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {
return;
}
$usr= $this->getUser();
if (is_object($usr)) {
if ($this->maxIdleTime > 0) {
$this->session->start();
$lapse = time() - $this->session->getMetadataBag()->getLastUsed();
if ($lapse > $this->maxIdleTime) {
$this->securityContext->setToken(null);
$this->session->getFlashBag()->set('info', 'You have been logged out due to inactivity.');
}
}
}
}
public function onKernelRequest(GetResponseEvent $event)
{
$this->maxIdleTime = ($this->container->getParameter('session_max_idle_time') > 0) ? $this->container->getParameter('session_max_idle_time') : 1200;
if (HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {
return;
}
$usr= $this->getUser();
if (is_object($usr)) {
if ($this->maxIdleTime > 0) {
$this->session->start();
$lapse = time() - $this->session->getMetadataBag()->getLastUsed();
if ($lapse > $this->maxIdleTime) {
$this->securityContext->setToken(null);
$this->session->getFlashBag()->set('info', 'You have been logged out due to inactivity.');
}
}
}
}
它在特定时间后正确到期会话,但问题是,它不会立即使会话过期,而是在页面上向前移动一步,当我回击时,会使会话到期。
我无法理解是什么原因。请帮助为什么它在下一页移动然后从那里到期会话并返回登录页面。
答案 0 :(得分:1)
设置后解决了这个问题。
$这 - >会话而>无效();