Метод 1: использование session.gc_maxlifetime
// Set the session timeout period
ini_set('session.gc_maxlifetime', 1800); // 1800 seconds = 30 minutes
// Start the session
session_start();
// Check if the session has expired
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > ini_get('session.gc_maxlifetime'))) {
// Session expired, destroy session and redirect to login page
session_unset();
session_destroy();
header("Location: login.php");
exit;
}
// Update the last activity time stamp
$_SESSION['LAST_ACTIVITY'] = time();
Метод 2. Использование специального значения тайм-аута сеанса
// Set the session timeout period
$session_timeout = 1800; // 1800 seconds = 30 minutes
// Start the session
session_start();
// Check if the session has expired
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > $session_timeout)) {
// Session expired, destroy session and redirect to login page
session_unset();
session_destroy();
header("Location: login.php");
exit;
}
// Update the last activity time stamp
$_SESSION['LAST_ACTIVITY'] = time();
Метод 3: использование session.cookie_lifetime
// Set the session cookie lifetime
ini_set('session.cookie_lifetime', 1800); // 1800 seconds = 30 minutes
// Start the session
session_start();
// Check if the session has expired
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > ini_get('session.cookie_lifetime'))) {
// Session expired, destroy session and redirect to login page
session_unset();
session_destroy();
header("Location: login.php");
exit;
}
// Update the last activity time stamp
$_SESSION['LAST_ACTIVITY'] = time();
Это всего лишь несколько примеров того, как можно реализовать функцию тайм-аута сеанса в PHP. Не забудьте настроить значения тайм-аута в соответствии с вашими конкретными требованиями.