Php 会话注销处理程序存在错误
我试图实现会话注销功能,但我的实现似乎有缺陷。对于初学者来说,超时是不一致的,似乎在不同的时间注销用户。此外,当用户注销时,它不会将他们重定向到我指定的登录URL,而只是停留在屏幕上,用户知道他们注销的唯一方式是单击链接 请尽我所能提供帮助,并提前感谢您的建议:Php 会话注销处理程序存在错误,php,symfony,session-timeout,symfony-2.3,Php,Symfony,Session Timeout,Symfony 2.3,我试图实现会话注销功能,但我的实现似乎有缺陷。对于初学者来说,超时是不一致的,似乎在不同的时间注销用户。此外,当用户注销时,它不会将他们重定向到我指定的登录URL,而只是停留在屏幕上,用户知道他们注销的唯一方式是单击链接 请尽我所能提供帮助,并提前感谢您的建议: <?php namespace Main\UserBundle\Component\Session\Handler; use Symfony\Component\HttpKernel\HttpKernelInterface;
<?php
namespace Main\UserBundle\Component\Session\Handler;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
class SessionIdleHandler implements EventSubscriberInterface {
protected $session;
protected $securityContext;
protected $router;
protected $maxIdleTime;
public function __construct(SessionInterface $session, SecurityContextInterface $securityContext, RouterInterface $router, $maxIdleTime = 0) {
$this->session = $session;
$this->securityContext = $securityContext;
$this->router = $router;
$this->maxIdleTime = $maxIdleTime;
}
public function onKernelRequest(GetResponseEvent $event) {
if (HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {
return;
}
$request = $event->getRequest();
if (!$request->hasSession()) {
return;
}
$session = $this->session;
$session->start(); //on every page request the session is restarted
$session_data = $session->getMetadataBag();
$lapse = time() - $session_data->getLastUsed();
if ($lapse > $this->maxIdleTime) {
$session->invalidate();
$this->securityContext->setToken(null);
$this->session->getFlashBag()->set('info', 'You have been logged out due to inactivity.');
$event->setResponse(new RedirectResponse($this->router->generate('fos_user_security_login')));
}
}
public static function getSubscribedEvents() {
return array(
KernelEvents::REQUEST => array('onKernelRequest', 127),
);
}
}
在我的parameters.yml中
session_max_idle_time: 3600
session_max_idle_time: 3600