checkSessionTimeout()
Purpose: Automatically logs out a user after a period of inactivity.
php
function checkSessionTimeout()
{
global $pdo;
// 10 seconds for testing
// 1800 seconds = 30 minutes
$timeout = 1800;
if (isset($_SESSION['last_activity'])) {
$inactive = time() - $_SESSION['last_activity'];
if ($inactive >= $timeout) {
endUserSession($pdo);
session_unset();
session_destroy();
header(
'Location: ' .
BASE_URL .
'/index.php?timeout=1'
);
exit;
}
}
$_SESSION['last_activity'] = time();
}
global $pdo;– Brings the PDO connection into scope (it's assumed to be defined outside this function).- Sets
$timeoutto1800seconds (30 minutes). The comment mentions 10 seconds is used for testing. - If
last_activityexists in the session:
Computes - Always updates
$_SESSION['last_activity']to the current time so activity keeps the session alive.
$inactive = time() - $_SESSION['last_activity']
If inactive ≥ timeout:
Calls endUserSession($pdo) to close the DB record.
Clears the PHP session with session_unset() and session_destroy()
Redirects to the login page with a ?timeout=1 flag.
exit stops further script execution.
Sign in to save your progress permanently.
Progress is saved in your browser session
Step 7 of 8