3

I am trying to force a time out after a certain number of seconds however even though the logout works, I am trying to implement a auto redirect to a URL which I am unable to implement. Any ideas?

I am following the example given on (the answer with the most votes): How to log users off automatically after a period of inactivity?

Community
  • 1
  • 1
kratos
  • 2,465
  • 2
  • 27
  • 45
  • So you are trying to redirect not logout? Also you want this to happen regardless of them making an additional request? IE a user sits on a page for 20 mins then is redirected? – Chase Mar 06 '14 at 23:13
  • I want to do both a logout and an automatic redirect/refresh so that the page can send the user to a login screen. – kratos Mar 09 '14 at 13:09

1 Answers1

4

That guide will only log you out after you make another request to the server. Meaning the page you are on currently could remain open forever.

It would be pretty easy to force a request to the server after a given time. In your template you can add a meta refresh:

<meta http-equiv="refresh" content="300">

Putting this in your header section of your html will reload the page after 5 minutes. The number is in seconds.

And then in the SessionIdleHandler class you would just change the response to redirect where you wanted.

from:

$event->setResponse(new RedirectResponse($this->router->generate('fos_user_security_login')));

to:

$event->setResponse(new RedirectResponse($this->router->generate('my_bundle.my_route')));
Chase
  • 9,289
  • 5
  • 51
  • 77
  • 2
    You could also use `$response->headers->set('Refresh', 300);` instead of the `meta http-equiv` tag. That way the logic for that is in the controller instead of the view. – Adam Elsodaney Mar 09 '14 at 21:46
  • Very true. IMO harder to track down where that little bit of code would be coming from down the road but it would work. – Chase Mar 10 '14 at 07:59
  • where would I set the $response->headers->set('Refresh', 300); since that needs to be applicable for every action. Maybe meta tag is better option? – kratos Mar 19 '14 at 12:19