Check is 10 minutes passed

Viewed 81

I need to check if a the time between session_start and current time exceeded 10 minutes. I've tried this:

$session_duration_max = 10; // 10 min
$current_time = time();

if ((time() - $_SESSION['session_start']) > $session_duration_max ) {
   // session expired
}

// elsewhere I set session
$_SESSION['session_start'] = time();

But I keep getting 0 when I subtract time() - $_SESSION['session_start'])

What am I missing?

1 Answers

time() is measured in seconds. To correctly set the timeout after 10 minutes, you must multiply the timeout duration by 60 seconds.

// Session does not exist
if(!$_SESSION) {
    // First Session: 
    $_SESSION['session_start'] = time();
}

$session_duration_max = (10 * 60); // 10 min

// Check if current time is larger than timeout 
if ((time() - $_SESSION['session_start']) > $session_duration_max ) {
   // session expired
   // delete session and require 
}

Above is a better idea of a system that would make sure timeout is honored.

Related