Log a user out on closing of a tab or browser

When closing a browser/tab close a session.

$(document).ready(function () {
$('form').submit(function () {
$('a').on('mousedown', stopLinkNav);

$('a').on('mouseleave', function () {
$(window).on('beforeunload', function () {
return 'Are you sure you want to leave?';

function stopLinkNav() {
$(window).on('unload', function () {
async: false,
url: "logout_ajax.php"


PHP FILE: logout_ajax.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.