Jump to content
Larry Ullman's Book Forums

Recommended Posts

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 ?



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:
$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.
       setcookie (session_name(), '', time()-86400, '/'); // Destroy the cookie.
       header("Location: $logout_redirect_url");
$_SESSION['start_time'] = time();

Link to comment
Share on other sites

  • 1 month later...

The following code has been working for me. I have placed this in the config file.


//create a session
if (!isset($_SESSION)) {
//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');
}else {
 //otherwise, set the value to the current time
$_SESSION['last_activity'] = $now;

Link to comment
Share on other sites


  • Create New...