Log a user out on closing of a tab or browser

When closing a browser/tab close a session.
[javascript]
$(document).ready(function () {
$(‘form’).submit(function () {
$(window).unbind(‘beforeunload’);
});
$(‘a’).on(‘mousedown’, stopLinkNav);
$(‘a’).on(‘mouseleave’, function () {
$(window).on(‘beforeunload’, function () {
return ‘Are you sure you want to leave?’;
});
});
function stopLinkNav() {
$(window).off(‘beforeunload’);
}
$(window).on(‘unload’, function () {
$.ajax({
async: false,
url: "logout_ajax.php"
});
});
});
[/javascript]
PHP FILE: logout_ajax.php
[php]
// php code to log user out here. or really do anything you might want to do
// good way to test it would be to log something, make sure its happening on closing of tab only and not just submitting a form or clicking a link, or changing url in the url bar.
[/php]

Post a comment

Your email address will not be published. Required fields are marked *