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
- What are the potential security risks associated with using @ to suppress errors in PHP code?
- What best practices should be followed when handling file operations in PHP scripts to avoid database connection errors?
- What are the potential security risks of embedding username and password in PHP scripts for network drive access?