In PHP, what are some alternative methods for handling user session data and automatically deleting entries when a user leaves a page, aside from tracking time of last visit?
One alternative method for handling user session data and automatically deleting entries when a user leaves a page is to use AJAX requests to periodically check if the user is still active on the page. If the user has been inactive for a certain amount of time, you can then destroy their session data.
// Check if user is still active on the page
if(isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > 60)) {
// User has been inactive for 60 seconds, destroy session data
session_unset();
session_destroy();
}
// Update last activity time
$_SESSION['last_activity'] = time();
Related Questions
- What potential pitfalls should be considered when creating a webpage that displays different data based on user selection?
- How can the use of the "*" operator in SQL queries be a potential pitfall when working with PHP and databases?
- What are the common pitfalls to avoid when working with deeply nested arrays in PHP to prevent errors like "Undefined index"?