Prevent ajax calls from updating session timeout with CakePHP

544 Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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

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.