0

I am developing a website that shows who-is-online. I mean when a user is online, the website will show all his friend who is online at that momment.

I use this manner to determine online list: when you sign in the website, a new record will be inserted to a table named ONLINE_LIST. When you click to Sign out button, session will be deleted and the same to the corresponding record in ONLINE_LIST table.

The problem is, when user leave my website without clicking to Signout button, how do I delete his session from online_list table.?

Songokute
  • 687
  • 1
  • 9
  • 17

1 Answers1

0

You need to do this with JavaScript. You can use the event beforeunload. How to capture the browser window close event?

Community
  • 1
  • 1
The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
  • Even when user opens multi tabs? Event will be performed muiti times? – Songokute Oct 10 '12 at 01:36
  • You would need to track this internally. User logs in increment login count variable 1. User closes a tab, decrement. If login count variable reaches 0 then you know it was the last tab. – The Muffin Man Oct 10 '12 at 01:47
  • Yes, it is good way. But I think we should use jsp to count, right? Beside, this way have an problem, that is when the browse suddenly closed (i.e when it out of electricity, or error appscrash occured...). In this situtiation, user will still be online? – Songokute Oct 10 '12 at 02:06
  • You are using Java to count. You are using AJAX to communicate with the server. There's nothing you can do if the computer shuts off instantly. Normally with an authentication system it automatically logs you out if there is no activity after a certain amount of time. So even if the system reports there is two tabs open the user will be logged out after x amount of time regardless if there really is any tabs open or not. This way you won't report that the user has been online forever if their computer crashed or was shut down expectantly. – The Muffin Man Oct 10 '12 at 17:56