jquery - How to prevent CakePHP 3.0 from extending session timeout with ajax requests? -


how can prevent cakephp 3.x extending users session when background ajax calls made server? using jquery's $.ajax() well.

i have setinterval running once minute user notifications. application ehr , need maintain strict session timeout. notifications javascript made sessions unlimited because ajax calls extending sessions.

i thought saw in cakephp book few weeks ago can't seem find today.

thanks, daren

generally need handle on own, ie implement own timeout mechanism. how handle it, depends.

you want exclude ajax background activity only, need have access request object, , want handle possible. given prerequisites, i'd use dispatcher filter, can extend timeout depending on whether or not current request ajax request, , destroy session before controllers involved.

here's basic, pretty self-explantory example, assumes timeout option value set session configuration.

src/routing/filter/sessiontimeoutfilter.php

namespace app\routing\filter;  use cake\core\configure; use cake\event\event; use cake\routing\dispatcherfilter;  class sessiontimeoutfilter extends dispatcherfilter {     public function beforedispatch(event $event)     {         /* @var $request \cake\network\request */         $request = $event->data['request'];          $session = $request->session();         $lastaccess = $session->read('sessiontimeoutfilter.lastaccess');          if (             $lastaccess !== null &&             time() - $lastaccess > configure::read('session.timeout') * 60         ) {             $request->session()->destroy();         }          if (!$request->is('ajax')) {             $session->write('sessiontimeoutfilter.lastaccess', time());         }     } } 

src/config/bootstrap.php

dispatcherfactory::add('sessiontimeout'); 

depending on specific needs, can of course place similar code pretty anywhere in application have access request object.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -