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

Link to comment
Share on other sites

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].

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.