Automatically redirect user in session Timeout or inactive

I want the timer to run every 3 minutes on the page (javascript) to determine if the php session ($ _SESSION) will expire ... and if so, redirect them automatically.

A good example would be: a user logs in and climbs a ladder and never returns. I want javascript to log out with a simple redirect ...

Is it possible? and how can i do this? I am using PHP and javascript.

Edit: What Rob said below is exactly what I'm looking for ... and I calmly quote ...

I suspect what Mike is asking for is that when the session ends, the browser will need to go from the current page. For example, some banks do this after a period of inactivity. - Rob Kennedy 5 hours ago

+6
javascript php session
source share
3 answers

You can use simple meta-update:

<meta http-equiv="refresh" content="180;url=http://example.com/logout" /> 

Or you implement a timeout with PHP:

 session_start(); if (isset($_SESSION['LAST_REQUEST_TIME'])) { if (time() - $_SESSION['LAST_REQUEST_TIME'] > 180) { // session timed out, last request is longer than 3 minutes ago $_SESSION = array(); session_destroy(); } } $_SESSION['LAST_REQUEST_TIME'] = time(); 

Then you do not need to check every 3 minutes if the session is still valid.

+9
source share

New and improved solution

As Mr. Kennedy noted, my initial solution (see below) does not work. so here is a way to do it.

The user’s database stores the timestamp of the last action, which is updated every time the user loads the page.

Then in checkaccess.php

 if ( time-last_access > max_inactivity_time ) { return array('access' => '0'); } else { return array('access' => '0'); } 

Call checkaccess.php in the javascript timer (below) and log out accordingly

It also allows you to use the "currently logged in" feature.

thanks mr kennedy


Original, non-working solution

Create a php page that returns 1 or 0 depending on the validity of the current user session.

Then on your pages that you want to disable, add this to the head (you need jquery)

 setInterval(function(){ var url = UrL_OF_SESSION_CHECKING_PAGE; $.getJSON( url, function( data ) { if (data.access=='0') { window.location = LOGIN_PAGE; } } ); }, 180000); 

Every 180 seconds (3 minutes) it requests a php page and gets a session validity. If it is invalid, it is redirected to the login page

If the user opens several pages, the pages will timeout and redirect at different times, because their timers are different.

Here's a nice page about javscript timers http://ejohn.org/blog/how-javascript-timers-work/

Simple session verification page

 session_start(); die( json_encode( isset( $_SESSION['VARIABLE'] ) ? array( 'access' => '1') : array( 'access' => '0' ) ) ); 

change the VARIABLE value to one of your session variables

+1
source share

If you want this to happen before the page has been refreshed, you will need periodic ajax calls. You can use jQuery Heartbeat to make calls every 3 minutes and use one of the PHP methods already provided by other users to check the session

0
source share

All Articles