I want to run a query on browser close that sets online users' status from 1 to 0.
I searched around and found two methods and want to know what is better.
Method 1:
window.onbeforeunload = function() {
$.ajax({
url: 'logout.php',
type: 'GET',
async: false,
timeout: 4000
});
};
Method 2:
$(window).unload(function() {
$.ajax({
url:"logout.php",
type:"POST",
async:false, // browser waits till xhr completed
success:function() {
alert("bye!");
}
});
});
Logout.php
mysql_query("UPDATE `users` SET `status` = '0' WHERE `user_id` = ".$session_user_id."");
All help is appreciated!