views:

162

answers:

2

Hello, I'm using CakePHP in one website and I was wondering how can I automagically redirect when the session has expired?

A: 

Have you tried to google for the answer.

Google for it!

This one looks like a solid lead.

mediaslave
Sure, I know how this stuff works, my question was redirecting the user the very second that the session expires.
Juanda
+3  A: 

The session expiration is equivalent to the user logging out of your app. You can set variable

$this->Auth->logoutRedirect = array('component'=>'YourComponent','action'=>'YourAction');

and this will achieve similar results. You want set this in the beforeFilter() of your AppController.

If you want to redirect the user the very second the session expires, you will need to roll some custom javascript to achieve this effect. You could start with determining the approximate number of seconds until the session expires, passing it to a javascript setTimeout() call and firing a function which forces the user to logout. There are some caveats to this approach, but it would work just as well.

Michael
Excelent, thanks a lot!It makes sense. However, what if the user has Javascript disabled?
Juanda
Then you won't be able to force the user to log out. You'll just have to wait for them to make their next request from CakePHP. At that point, Cake will see the session has expired and force them to logout at that time, redirecting them to whatever you set in `$this->Auth->logoutRedirect`. :)
Michael