I'm using CakePHP in one website and I was wondering how can I automagically redirect when the session has expired?
2 Answers
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.
-
Excelent, thanks a lot! It makes sense. However, what if the user has Javascript disabled?– JuandaJul 28, 2010 at 12:37
-
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
. :) Jul 28, 2010 at 18:35
using your components ($components) you choose the login specifics details and lougout too such as the redirect page when the session close.
public $components = array(
'Session'=>array(
'timeout' => 620
),'Auth' => array(
'loginRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
)
)
);