How can PHP be used to check if a user has been inactive for a certain amount of time?
To check if a user has been inactive for a certain amount of time in PHP, you can store a timestamp of the user's last activity in a session variable. Then, you can compare the current time with the stored timestamp to determine if the user has been inactive for the specified amount of time.
session_start();
$inactive_time = 60; // 1 minute of inactivity
if(isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity']) > $inactive_time) {
// User has been inactive for the specified amount of time
// Perform any necessary actions, such as logging the user out
session_unset();
session_destroy();
}
$_SESSION['last_activity'] = time();
Keywords
Related Questions
- How can one troubleshoot issues with queries not returning results in PHP?
- What best practices should be followed when reading and processing data from a file in PHP to avoid potential pitfalls like memory consumption or file handling errors?
- What are the potential pitfalls of using a Page Builder like Thrive Architect for creating HTML forms with PHP functionality?