How to make online/offline

房东的猫 提交于 2019-12-01 23:10:33

问题


I have a login system. With my login session stores and cookies (if you "remember me") and then you login. When you're inside the login system at home.php (you can only enter if session/cookie is registered), i want the status field in "user" table, to update to online and offline when you sign out.

I know how to do this, to update the status when you login and log out, but here's the thing: How do i do when the user leaves the site without logging out (pressing on the actual log out button) ? I mean if the status is set to Online and you leave the page, it will remain Online you may understand..logic.. so what do i do at this situation? I don't want to have a little ugly popup that log you off when you leave the page, that's so old school and bad.

Or maybe when the session runs out then you set the status to offline? but again, how should i do that?

Ideas and examples on solution for this would be good.

Sites like e.g facebook, in the Chat you change status to Idle if you leave the pages, and somehow if you leave the page and there goes some time you are offline... How, I don't know.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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)



回答6:


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




回答7:


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.



来源:https://stackoverflow.com/questions/3435638/how-to-make-online-offline

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