Prevent ajax calls from updating session timeout with CakePHP

泄露秘密 提交于 2019-12-24 05:46:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!