Thursday, October 5, 2023
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  22] [ 4]  / answers: 1 / hits: 17258  / 15 Years ago, tue, june 16, 2009, 12:00:00

I want to have a timer going to run every 3 minutes on the page (javascript), to detect if a php session ($_SESSION) has timed out... and if so, redirect them automatically.


A good example would be, a user logs in and runs up stairs, and never comes back down... I want the javascript to log them out with a simple redirect...


Is this possible? and how would I do such a thing? I am using PHP and JavaScript.


What Rob Kennedy said below is exactly what I am looking for:



...when the session times out,
the browser should be told to navigate away from the current page.
Some banks do this after a period of inactivity, for example.



More From » php

 Answers
2

You could use a simple meta refresh:



<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 don’t need to check every 3 minutes if the session is still valid.


[#99302] Friday, June 12, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
victorw

Total Points: 484
Total Questions: 120
Total Answers: 107

Location: Faroe Islands
Member since Thu, Apr 8, 2021
3 Years ago
;