smc Posted January 22, 2007 Share Posted January 22, 2007 Hiya,How can I make it so a session expires after, say, 2 hours? As it stands right now it seems like as long as the browser is open the session is open. I just want to add some additional security.Thanks!! Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 You could record the time the session starts as a variable, and on each page check if it's been two hours? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 22, 2007 Share Posted January 22, 2007 Here is what did:[code]<?php//No session set (first time user visited)if(empty($_SESSION['last_active'])){ //Set the Session $_SESSION['last_active'] = time();}//Session set, but user not active for more than thirty minuteselseif(!empty($_SESSION['last_active']) && $_SESSION['last_active'] < time()-7200){ print "You must be logged in to view this page. <a href='index.php'>Login here.</a>"; //Destroy the session $_SESSION['last_active'] = ''; exit;}//Session set, and the user has been active for under thirty minuteselseif(!empty($_SESSION['last_active']) && $_SESSION['last_active'] >= time()-1800){ //Update session with current time $_SESSION['last_active'] = time();}?>[/code]Put this on your header.php page so it executes every time somone clicks. I changed it for you so it keeps them logged in for 2 hours. Quote Link to comment Share on other sites More sharing options...
smc Posted January 22, 2007 Author Share Posted January 22, 2007 Awesome, would it work if I put it in a sessiontime.php and included it on the files? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 22, 2007 Share Posted January 22, 2007 It should ^^ Just make sure you include it at the top under your session_start() and there shouldn't be a problem. Quote Link to comment Share on other sites More sharing options...
smc Posted January 25, 2007 Author Share Posted January 25, 2007 Edit: Nevermind, it does ;D Quote Link to comment Share on other sites More sharing options...
BenD Posted March 27, 2009 Share Posted March 27, 2009 Thanks so much pocobueno1388, I place the code under session_start() like you indicated it works like charm. Thanks again for sharing. B. Quote Link to comment 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.