PHP Session Timeout
As you're creating a session during user login, you can set a timeout duration for inactivity and perform a specified action upon its expiration. Here's how to achieve this:
Last Request Timestamp
Store the timestamp of the user's last request in the session variable on each request:
$_SESSION['timeout'] = time();
Session Expiry Check
On subsequent requests, you can check how long ago the previous request was made. For instance, if you want to timeout the session after 10 minutes of inactivity:
if ($_SESSION['timeout'] + 10 * 60 < time()) { // Session timed out // Perform desired action (e.g., function execution or page redirect) } else { // Session is active }
Remember that the session will time out after 10 minutes of inactivity, and you can customize this duration as per your requirements.
The above is the detailed content of How Can I Implement PHP Session Timeout Based on User Inactivity?. For more information, please follow other related articles on the PHP Chinese website!