问题
I'm using some long pooling in JavaScript of this kind:
setInterval(demo, 3000);
function demo(){
$.get(url, params, function(data){
//whatever
});
}
Being url
a URL to a CakePHP controller action returning JSON.
But I want my session to only last 20 minutes since the user last action on the screen. This is, ignoring the pooling which is taking place every 30 seconds. Otherwise the session will last forever.
Any solution to this?
回答1:
Please use this in app controller in beforeFilter function
if(!$this->request->is('ajax')){
$LastActivity = $this->Session->read('LastActivity');
if ($LastActivity != '' && (time() - $LastActivity) > 1200) {//for 20 minute
$this->Auth->logout();
$this->redirect('/');
}
$this->Session->write('LastActivity', time());
}
回答2:
Store the last login time in the session when a request happens compare it to the current time when a request comes in. If the current time is greater than last login time + 20min call the logout() method of the auth component.
来源:https://stackoverflow.com/questions/37948391/prevent-ajax-calls-from-updating-session-timeout-with-cakephp