honjian5231 Posted November 21, 2012 Share Posted November 21, 2012 Hi ,this book are really good , simple and easy to understand , don't have any problem on understanding your words so far. I have apply to two of my project . This is not really a problem , just not sure where to start off . I was trying to improve the login system , because when the user forget to click the logout button , the session doesn't logout automatically.What i trying to do is make a session expire after 15 minutes of inactivity ? Any ideas ? [sOLVED] used this solution apply to include/config.inc.php just after the session_start() Hope this can helps $live = false; // Errors are emailed here: $contact_email = 'abc@gmail.com'; define ('BASE_URI','.'); define ('BASE_URL','www.domain.com/'); define ('MYSQL', BASE_URI . '/include/mysql.inc.php'); // Start the session: session_start(); $timeout = 15; // Set timeout minutes $logout_redirect_url = "index.php"; // Set logout URL $timeout = $timeout * 60; // Converts minutes to seconds if (isset($_SESSION['start_time'])) { $elapsed_time = time() - $_SESSION['start_time']; if ($elapsed_time >= $timeout) { $_SESSION = array(); // Destroy the variables. session_destroy(); setcookie (session_name(), '', time()-86400, '/'); // Destroy the cookie. header("Location: $logout_redirect_url"); } } $_SESSION['start_time'] = time(); Link to comment Share on other sites More sharing options...
Edward Posted November 21, 2012 Share Posted November 21, 2012 Check out session.gc-maxlifetime: http://php.net/manua....gc-maxlifetime Actually there is talk on how this is not reliable here is more information that should solve your problem: http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes Link to comment Share on other sites More sharing options...
honjian5231 Posted November 21, 2012 Author Share Posted November 21, 2012 I did read about this in stackoverflow...still you want to suggest this bad solution since its not reliable? Updated my homework. What if i wan to use this code?meet the logic? where should i put to get it work? Link to comment Share on other sites More sharing options...
Marie Posted January 4, 2013 Share Posted January 4, 2013 The following code has been working for me. I have placed this in the config file. //create a session if (!isset($_SESSION)) { session_start(); } //store the current time $now = time(); // get the time the session should have expired $limit = $now - 60 * 30; // check the time of the last activity if (isset ($_SESSION['last_activity']) && $_SESSION['last_activity'] < $limit) { // if too old, clear the session array and redirect $_SESSION = array(); header('Location: http://xxxxxxxxxxxx/expired.php'); exit; }else { //otherwise, set the value to the current time $_SESSION['last_activity'] = $now; } Link to comment Share on other sites More sharing options...
Recommended Posts