How to make online/offline

柔情痞子 提交于 2019-12-01 21:11:30

Facebook is a bit nasty in that their page contains some Javascript that keeps an open connection to their chat server. Once the connection is lost, it means you've closed the page (or your internet connection) and you're marked offline.

Usually, just a timeout is used that marks the user offline some time after their last activity/page load. A reasonable value for this timeout could for instance be the time after which their session cookie expires.

The simple solution is to save the time of the last access of a user and consider him as offline if he really logged-out or if that time is too long in the past.

For a better solution (similar to facebook) you need to use semi-persistent connnections and use them to detect presence with more granularity.

How do i do when the user leaves the site without logging out (pressing on the actual log out button) ?

Well, you can't detect that. The best you can do is a "last active" statistic or "users active in the last then minutes". In this case, you should update a database field each type the user makes a page request.

You can store a timestamp on pageload, then base online/offline status on the amount of time that has passed since.

You could have in the user's table a field called last_visit for example, and update it with the sysdate, now(), etc when he accesses any page...

In the query that gives you the online users, you will filter it with the users that have in the field last_visit ten minutes or less....

like:

where last_visit < today-(10 minutes)

I guess you should do something similar to AJAX push. @Wim suggests a similar approach as well.

I've been writing a site in php as a hobby recently, change from the norm and my take on this was, where sessions are being stored in files to:

  • Have a separate table and list user id, last_update time, session id
  • store a variable in the session called last_active
  • On each page call check if the last active time was less than 30 seconds (or whatever you want) if it's longer then update the table in step one, if not carry on. This stops you needing a call on every page and will be accurate to within a minute or so.

The session id in the database is unnecessary, just nice to see who's got what session id active, I even wrote a bit of a hackish function to delete the session files based on this id on a manual basis, if I wanted to log someone out... no real reason for it, just a bit of fun.

Of course if you're saving sessions in your database then it's even easier as you're doing a database trip every page anyway so updating it isn't that much of an issue.

You can also add some ajax to keep the db updated even when the page is open but not being actively refreshed then as someone else pointed out you check who's online by checking out whose last update database entry is within whatever arbitrary range you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!