I have a login-based site and I'd like to log the user out when they leave the site (i.e. close the browser or close the tab). I'm aware this isn't fool-proof by any means, but I want to set it up as best I can nonetheless.
I am using the following code:
$(window).unload(function() {
//Send an Ajax request to logout.php
$.ajax({
type: "POST",
url: "scripts/logout.php"
})
});
And logout.php
simply unsets the session.
This works fine when closing the website, but it works a little too well, meaning that it also fires when a page is reloaded or when an intra-site link is clicked. How can I prevent this behavior, i.e. check that the user's action is actually closing the browser or navigating away from the site, as opposed to reloading or visiting another site section?