How can one implement a session with a time limit in PHP?
To implement a session with a time limit in PHP, you can set a timestamp when the session is started and then check if the current time exceeds the time limit. If it does, you can destroy the session and redirect the user to a login page.
<?php
session_start();
$session_time = 60; // 1 minute time limit for the session
if (isset($_SESSION['start_time']) && time() - $_SESSION['start_time'] > $session_time) {
session_unset();
session_destroy();
header("Location: login.php");
exit;
}
$_SESSION['start_time'] = time();
?>
Keywords
Related Questions
- How can one troubleshoot and resolve errors related to undefined functions in PHP?
- In what part of the code is the current position of the chess pieces tracked, and how does it impact the movement of the pieces on the board during user input?
- How can the usage of global variables in PHP code impact readability and maintainability, and what are alternative approaches?