Forum Moderators: coopster
Till now i have registered the user and using session info i know if the user is logged or not. When he is logged on i update the database table field online =1 and otherwise it is 0 .(1 represents (s)he is online). It's fine till here... But next I have to check after 2 seconds if the user is still logged on or has logged out.
Suppose he is currently logged then the db field says 1, now he logs out but then also the db field says 1 although it should say 0 (as user logged out). SAo my problem is how to know if the user is still logged on or not.
If there something like pinging that can be done ... it could be helpful but wiill take alot of resources...
suggestion/comments/links welcome
> clicking on logout button, which in this case you don't have a problem. You can directly update the database.
> Just closing the browser - this is a more difficult one. For this, you need to find a way to do it through Ajax, I suggest. At the event of unloadig the explorer, you update the db as the person has logged out. There might be cases when you can't achieve this, and at those times you can let the chat expire if there is no activity for an hour or two.
Hope this helps.
Habtom
Javascript onUnload and onError could be used to log them out if there is an error or they just close the browser, as said above.
For a php solution you could check that they have sent any content within a given time and if they haven't then log them out. So add another column to your table with last_sent and put a time stamp in there and update this field when they send content through your instant messenger. Then you just need to check that the value of last_sent is less than last_sent+5 mins if you choose 5 mins as long enough.
This isnt perfect as if the person doesnt type anything but is reading a very long message then they may get logged out, however so long as they dont loose the message they are reading then they can log back in again to reply. Also the connection will be open for 5 mins if they close there browser, however at least the connection will close eventually :)