What are some alternative methods to ending a session in PHP besides using a logout button?
One alternative method to ending a session in PHP besides using a logout button is to set a session timeout. By setting a specific time limit for the session, the user will be automatically logged out after a certain period of inactivity. This can help improve security and protect user data.
// Set session timeout to 30 minutes
ini_set('session.gc_maxlifetime', 1800);
session_start();
// Check if session is expired
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
session_unset();
session_destroy();
}
// Update last activity time
$_SESSION['LAST_ACTIVITY'] = time();