2

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!

  • I vote for the first one. – iamsleepy Apr 04 '14 at 08:10
  • bear in mind that unload will be triggered, on reloading the page, navigating away from the page or even using the forward/backward history buttons browsers have. – MentalRay Apr 04 '14 at 08:11
  • Yes the first One is sutable in this case. – Rohit Nigam Apr 04 '14 at 08:12
  • [Get vs post in ajax](http://stackoverflow.com/questions/715335/get-vs-post-in-ajax) – Debflav Apr 04 '14 at 08:12
  • @RohitNigam What do you mean by "in this case". Is the first method good and not gonna effect speed or overload the server in some way? –  Apr 04 '14 at 08:18
  • @All if some one open app in two tabs then what will be happen .you close one tab and the you logout...? – HaRsH Apr 04 '14 at 08:20
  • I vote for the first one simply because I don't like the alert. – iamsleepy Apr 04 '14 at 08:21
  • @iamsleepy what if you remove success:function() {alert("bye!");} ? but I think I am sticking with the first one as well. I am just trying to figure out if it's the right way to do it without overloading or slowing things down –  Apr 04 '14 at 08:24
  • 1
    @GoGo I mean that the first one is suitable to check the user is online/ideal. – Rohit Nigam Apr 04 '14 at 08:24
  • @HaRsH great point. Is there a way to fix that so it doesn't do it as long as the website is open? –  Apr 04 '14 at 08:25
  • how do I check which user is online (and which one closed their browser)? my query seems to not do anything –  Apr 04 '14 at 08:27
  • @GoGo I don't think the difference will be significance in this case. If you want to know almost realtime user status. You can use ajax to fire the user status to server every 5 seconds (not a very good idea though.). Or you can use `websocket`. – iamsleepy Apr 04 '14 at 08:30
  • @GoGo i am just trying ...set something cookie when there is no cookie logout or you can do it by session also...but i have a doubt in one case or question that any session or cookie will be only when the tabs or tab is open or not....big shuck\ – HaRsH Apr 04 '14 at 08:31
  • yeah I think session is out of the question, and cookie would not make sense to work on other tabs as I would assume it saves on its own created tab. any idea how to run my query properly just to get the status to switch for whatever user is inactive or logs out? –  Apr 04 '14 at 08:38
  • Why do you need to know the moment a user logs out? – Celos Apr 04 '14 at 11:42
  • well I want their status to be 0 as I have a chat system that shows green and grey on users and if it's green, it would be assumed he is online. –  Apr 04 '14 at 18:02

2 Answers2

0

To check for online users you should use some other thing, if they forcefully close their browser your script won't be able to detect it.

To check for online users, you should send some data to them every 5 minutes, if data is reached, then they are online else they are not. You should make your system something like this instead of that.

user1735921
  • 1,359
  • 1
  • 21
  • 46
0

I have a solution for this use following code.

<script type="text/javascript">
    window.onbeforeunload = function() {
        $.ajax({
            url: URL+param,
            type: 'GET',
            async: false,
            timeout: 10000
        });
    };
</script>
Dayachand Patel
  • 469
  • 4
  • 12