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();