-2

In my database i inserted a column named "online" with a default value of 0. So when a user log in this value is updated to 1 so now it says the user is online. When a user click on log out this value gets updated to 0 again so now the user is offline. But i was thinking, what if the user just closed the browser without signing out, nothing will get updated and the user will stay online. Can you please help with guidance to how can I work this out ? Is there anyway of doing this with javascript or jquery ajax that detects when the browser is closed ?

firashelou
  • 131
  • 9
  • Are you looking for a auto log out script or just appear as logged out? – Andreas Jul 30 '18 at 17:05
  • The most basic way would be to rely on the concept of timing out. If you add a column for "last active datetime", and then have every page that they visit (eg when it authenticates that they're logged in) update that row with the current datetime. Then you can show that a user is offline if that datetime is, for example, 15 minutes or longer from the current datetime. – RToyo Jul 30 '18 at 17:05
  • @firashelou Why don't you use a session to store the online - offline status? – ash__939 Jul 30 '18 at 17:06
  • Thanks everyone for your suggestion i am building it in my head from your answers, well @RToyo i have a column in my table that is "last_update_timestamp" that can be used for this purpose but what if a user stays on a page for a long time and he still is online, then wouldn't that make his status says he is offline ? – firashelou Jul 30 '18 at 17:13
  • @invalid-bot you mean simply store a new value in the session for online ? and when he goes offline what would happen ? what should i do ? – firashelou Jul 30 '18 at 17:14
  • I misunderstood the question. On reading further comments, I think you want to show the online status of a user to another user. If so, you should use something like websockets as mentioned in @Meghan's answer. Otherwise, add a field in the database, and update it at regular intervals with a script. While checking the online status, check if the timestamp is within the specified limit. – ash__939 Jul 30 '18 at 17:23
  • @invalid-bot i could not understand what is websockets ? – firashelou Jul 30 '18 at 17:31
  • @firashelou https://www.maxcdn.com/one/visual-glossary/websocket/ Read about websockets here. If you're not sure how to implement this, go for the second option. Use a javascript and Ajax. – ash__939 Jul 30 '18 at 17:35
  • ok thank you @invalid-bot but what about ajax and javascript how is that done ? – firashelou Jul 30 '18 at 17:40
  • @firashelou Meghan's answer will give you accurate results, while my simple suggestion for timeouts simply works on the principal of "we don't know if they're still online, so just make an educated guess". The guess is that if there's no new activity after 15 minutes, then it means that the user either closed their browser, or left their browser open and walked away from their computer. – RToyo Jul 30 '18 at 17:54
  • @firashelou Add an ajax call that will update the database field with the current timestamp for the specific user. Then call that function at regular intervals, say every 2 minutes. Where you display the login status, check whether the timestamp is within a limit, say 5 minutes, if not, show the user as inactive. – ash__939 Jul 30 '18 at 18:12
  • if you are using per request sessions to track the user state You can create a cron job or database event to compare the difference in seconds between the last action time from the current time and your `session.gc_maxlifetime` setting *(default 1440)*. `UPDATE WHERE TIME_TO_SEC(TIMEDIFF(NOW(), last_action_date)) >= 1440` . So when the user closes their browser/tab, or sits on a page too long, the status is updated to offline when their session state is set to expire. Relying solely on the client side to track the session/logged in state will lead to inconsistent data. – Will B. Jul 30 '18 at 18:12

1 Answers1

3

You can use onbeforeunload but this may prove to be inconsistent.

If you always want to know if the user is connected to your service, you may want to look into using WebSockets with socket.io or similar.

Meghan
  • 1,215
  • 11
  • 17
  • I can't understand what is this websockets ? – firashelou Jul 30 '18 at 17:32
  • @firashelou http://blog.teamtreehouse.com/an-introduction-to-websockets – ash__939 Jul 30 '18 at 17:37
  • I found this tutorial on youtube it's a 9 videos tutorial https://www.youtube.com/watch?v=OHtVeuwxJDA I am not sure if i understood how the status turns from on to off but i managed to implemented in my application and it works great, let me know your opinions please – firashelou Jul 31 '18 at 18:10