playaz Posted November 16, 2009 Share Posted November 16, 2009 Hi guys, I know how to use sessions ok, I just wondered what is the best/simplest way to automatically destroy a session after a set time if there is no user activity on a page (eg like how banking sites log you out etc..) Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/181711-automatically-logout-after-15-mins-via-sessions/ Share on other sites More sharing options...
isedeasy Posted November 16, 2009 Share Posted November 16, 2009 Not sure about this but I would have thought that changing session.cookie_lifetime = 0 to session.cookie_lifetime = 900 in your php.ini file would make sessions expire after 15 minutes. Link to comment https://forums.phpfreaks.com/topic/181711-automatically-logout-after-15-mins-via-sessions/#findComment-958429 Share on other sites More sharing options...
rlelek Posted November 16, 2009 Share Posted November 16, 2009 Hey there... Here's something I came up with that'll work just fine. The script in its current state isn't fort knox (actually, you can beat the login again by clearing your browser cookies). I did make this script and usaully what happens in these forums is we only hint at you to find the answer yourself. I actually solved the problem for you, and I don't mind, but please do me and the other forum posters one thing. ***** Learn From The Script***** Do not just implement it and never give it a second thought. Anyway, here it is, functional in full...And well documented! <?php // Start or Resume a Session session_start(); // How long is the user allowed to be idle? $idle = (15 * 60); // In Seconds // See if the User has been idle for too long // If he/she has, then log them out. // Otherwise, update their status. if(isset($_SESSION['time'])){ // Debug debug($idle); // How long from his/her last visit? $time_elapsed = (time() - $_SESSION['time']); if($time_elapsed < $idle){ // Update user's activity $_SESSION['time'] = time(); echo 'Activity Updated'; }else{ // He/She has been idle for too long echo 'You have been idle for too long<br />Please login again'; // Do Your Logout Function Here } }else{ // This is his/her first visit. $_SESSION['time'] = time(); echo 'First Visit'; // Possibly Require Login Here } function debug($idle){ echo '<br />------- Debug -------'; echo '<br />Time: '.time(); echo '<br />Last Activity: '.$_SESSION['time']; echo '<br /> Time Elapsed: '.(time() - $_SESSION['time']); echo '<br />Idle Time: '.$idle; echo '<br />----- End Debug -----<br /><br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/181711-automatically-logout-after-15-mins-via-sessions/#findComment-958457 Share on other sites More sharing options...
playaz Posted November 16, 2009 Author Share Posted November 16, 2009 Cheers rlelek.. will have another look at that 2mrw when I get some free time - much appreciated P.S I always learn from you forum guys hence the reason I'm here lol :-) Thanks again P.P.S isedeasy, your input also is appreciated too :-) Link to comment https://forums.phpfreaks.com/topic/181711-automatically-logout-after-15-mins-via-sessions/#findComment-958497 Share on other sites More sharing options...
isedeasy Posted November 16, 2009 Share Posted November 16, 2009 Yeah I guess if you just edited the php.ini it would end the session even if you were not idle. I am here to learn as well Link to comment https://forums.phpfreaks.com/topic/181711-automatically-logout-after-15-mins-via-sessions/#findComment-958509 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.