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

darensipes

How can I prevent CakePHP 3.x from extending my users session when background ajax calls are made to the server? I am using jquery's $.ajax() as well.

I have a setInterval running once a minute to get some user notifications. My application is an EHR and I need to maintain strict session timeout. My get notifications Javascript basically just made my sessions unlimited because the ajax calls are extending the sessions.

I thought a saw something in the CakePHP book about this a few weeks ago but I can't seem to find it today.

Thanks, Daren

ndm

Generally this is something that you need to handle on your own, ie implement your own timeout mechanism. How to handle it, depends.

You want to exclude AJAX background activity only, so you need to have access to the request object, and you most probably want to handle this as early as possible. Given this prerequisites, I'd probably use a dispatcher filter, where you can extend the timeout depending on whether or not the current request is an AJAX request, and destroy the session before any controllers are involved.

Here's a very basic, pretty much self-explantory example, which assumes that the timeout option value is set for the 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 your specific needs, you can of course place similar code pretty much anywhere in your application where you have access to the request object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Prevent ajax calls from updating session timeout with CakePHP

From Dev

Why are my AJAX requests not extending an OWIN MVC session?

From Dev

How to alter laravel middleware so failed ajax requests due to session timeout kick to login

From Dev

How do I prevent Ajax calls from keeping a session alive?

From Dev

CakePHP - session timeout

From Dev

Prevent htaccess from affecting ajax requests

From Dev

Prevent ajax requests from timing out (jquery)

From Dev

Prevent htaccess from affecting ajax file requests

From Dev

How to prevent Chrome to redirecting AJAX requests to HTTPS?

From Dev

How to prevent Chrome to redirecting AJAX requests to HTTPS?

From Dev

Session timeout not working in Cakephp 2

From Dev

Prevent Expressjs from creating a session when requests contain an authorization header?

From Dev

How to prevent timeout(...) from cancelling the stream emission?

From Dev

How to limit users to one session with CakePHP 3?

From Dev

Cakephp 3 How to make session array

From Dev

How to check session in CakePHP 3.x

From Dev

How to prevent Laravel Routes from being accessed directly (i.e. non-ajax requests)

From Dev

How to start a php session() and access $_SESSION in CakePHP 3.x?

From Dev

SAS: prevent ENDPOINTS from extending

From Dev

Prevent div from extending scrollbar

From Java

Mobaxterm: how to prevent ssh session from exiting?

From Dev

Fancybox displays login form on session timeout cakephp

From Dev

Session timeout confusion - session.setMaxInactiveInterval(0)

From Dev

Session timeout confusion - session.setMaxInactiveInterval(0)

From Dev

How to set timeout for session?

From Dev

CakePHP SQL Log - How to check the SQL Logs from previous requests?

From Dev

How to get "data" from JQuery Ajax requests

From Dev

Cakephp 3.x how the get the session_id

From Dev

How to delete session for specific user in cakePHP3?

Related Related

  1. 1

    Prevent ajax calls from updating session timeout with CakePHP

  2. 2

    Why are my AJAX requests not extending an OWIN MVC session?

  3. 3

    How to alter laravel middleware so failed ajax requests due to session timeout kick to login

  4. 4

    How do I prevent Ajax calls from keeping a session alive?

  5. 5

    CakePHP - session timeout

  6. 6

    Prevent htaccess from affecting ajax requests

  7. 7

    Prevent ajax requests from timing out (jquery)

  8. 8

    Prevent htaccess from affecting ajax file requests

  9. 9

    How to prevent Chrome to redirecting AJAX requests to HTTPS?

  10. 10

    How to prevent Chrome to redirecting AJAX requests to HTTPS?

  11. 11

    Session timeout not working in Cakephp 2

  12. 12

    Prevent Expressjs from creating a session when requests contain an authorization header?

  13. 13

    How to prevent timeout(...) from cancelling the stream emission?

  14. 14

    How to limit users to one session with CakePHP 3?

  15. 15

    Cakephp 3 How to make session array

  16. 16

    How to check session in CakePHP 3.x

  17. 17

    How to prevent Laravel Routes from being accessed directly (i.e. non-ajax requests)

  18. 18

    How to start a php session() and access $_SESSION in CakePHP 3.x?

  19. 19

    SAS: prevent ENDPOINTS from extending

  20. 20

    Prevent div from extending scrollbar

  21. 21

    Mobaxterm: how to prevent ssh session from exiting?

  22. 22

    Fancybox displays login form on session timeout cakephp

  23. 23

    Session timeout confusion - session.setMaxInactiveInterval(0)

  24. 24

    Session timeout confusion - session.setMaxInactiveInterval(0)

  25. 25

    How to set timeout for session?

  26. 26

    CakePHP SQL Log - How to check the SQL Logs from previous requests?

  27. 27

    How to get "data" from JQuery Ajax requests

  28. 28

    Cakephp 3.x how the get the session_id

  29. 29

    How to delete session for specific user in cakePHP3?

HotTag

Archive