How can you redirect a user to a logout page after their session times out in PHP?
When a user's session times out in PHP, you can redirect them to a logout page by checking if the session is still active on each page load. If the session has expired, you can redirect the user to the logout page using header() function.
// Check if the session is still active
session_start();
if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > 1800)) {
// If session has expired, redirect to logout page
header("Location: logout.php");
exit;
}
// Update last activity time
$_SESSION['last_activity'] = time();
Keywords
Related Questions
- Welche Rolle spielt die Funktion headers_sent() bei der Verwendung von session_start() in PHP und wie kann sie dazu beitragen, potenzielle Probleme zu identifizieren?
- How can PHP be used to display a dynamically generated image on a webpage while still allowing other content to be displayed alongside it?
- What are some recommended resources for learning about PHP functions and their usage?