cluce Posted June 22, 2007 Share Posted June 22, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/56777-solved-trying-to-logout-user-after-five-minutes-of-inactivity/ Share on other sites More sharing options...
pocobueno1388 Posted June 22, 2007 Share Posted June 22, 2007 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]. Quote Link to comment https://forums.phpfreaks.com/topic/56777-solved-trying-to-logout-user-after-five-minutes-of-inactivity/#findComment-280410 Share on other sites More sharing options...
cluce Posted June 22, 2007 Author Share Posted June 22, 2007 thanks for teh reply but my first code did work. Quote Link to comment https://forums.phpfreaks.com/topic/56777-solved-trying-to-logout-user-after-five-minutes-of-inactivity/#findComment-280486 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.