cakephpcakephp-2.7

Prevent ajax calls from updating session timeout with CakePHP


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?


Solution

  • 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());
                }