How can PHP be used to detect and handle inactive or idle users on a website?
To detect and handle inactive or idle users on a website, you can set a timestamp when a user performs an action on the website and then check if the user has been inactive for a certain period of time. If the user is inactive for too long, you can log them out or redirect them to a specific page.
session_start();
$inactive_time = 300; // 5 minutes of inactivity
$session_expire = time() - $inactive_time;
if (isset($_SESSION['last_activity']) && $_SESSION['last_activity'] < $session_expire) {
// Redirect user to logout or inactive page
header("Location: logout.php");
exit;
}
$_SESSION['last_activity'] = time();
Keywords
Related Questions
- What are some potential pitfalls of using proprietary functions and methods in PHP code?
- Are there any specific PHP functions or techniques that can be used to efficiently locate and update a specific piece of text within a file, such as finding and editing a specific line of code?
- What is the best practice for passing variables via POST in PHP forms?