Jump to content

[SOLVED] trying to logout user after five minutes of inactivity


cluce

Recommended Posts

The code I found does not work.  I am trying to log out user after five minutes of idle time. here is what I have .

IAm including this on all pages...

<?php
$timeout_min = 5; //5 minutes of inactivity 
$timeout_length = $timeout_min * 60;
$current_time = time(); // get the current time

if ($current_time - $_SESSION['lastActivity'] > $timeout_length) {
$_SESSION = array();
if (isset($_COOKIE[session_name()])) {
   unset($_COOKIE[session_name()]);
}
        session_destroy();
	$_SESSION['logout']="You have been logged out";
	header ("Location: employee_resource.php");
exit;
}
else
$_SESSION['lastActivity'] = $current_time;
?>

and this code is on my login page to set the sessions....

	//sets login timer
$current_time = time(); // get the current time
    $_SESSION['loginTime']=$current_time; // login time
    $_SESSION['lastActivity']=$current_time; // last activity

your help is always apprecited ;D

You need to be updating the lastActivity session every time the user clicks...not just setting it when they login.

 

<?php

//No session set (first time user visited)
if(empty($_SESSION['lastActivity'])){
//Set the Session
$_SESSION['lastActivity'] = time();
}

//Session set, but user not active for more than 5 minutes
elseif(!empty($_SESSION['lastActivity']) && $_SESSION['lastActivity'] < time()-300){

//Clear their sessions/cookies

//Destroy the session
unset($_SESSION['lastActivity']);
exit;
}

//Session set, and the user has been active for under 5 minutes
elseif(!empty($_SESSION['lastActivity']) && $_SESSION['lastActivity'] >= time()-300){
//Update session with current time
$_SESSION['lastActivity'] = time();
}

?>

 

Put that on your header page code [script that is included at the top of every page].

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.