如何制作线上/线下
我有一个登录系统。 使用我的登录会话存储和cookie(如果您“记住我”),然后您登录。当您进入 home.php 的登录系统时(只有在注册了会话/cookie 的情况下才能输入),我希望“用户”表中的状态字段在您注销时更新为在线和离线。
我知道如何做到这一点,在您登录和注销时更新状态,但事情是这样的:当用户离开网站而不注销(按实际的注销按钮)时,我该怎么办?我的意思是,如果状态设置为在线并且您离开页面,它将保持在线状态,您可能会理解..逻辑..那么在这种情况下我该怎么办?我不想在您离开页面时出现一个丑陋的弹出窗口,让您注销,这太老套了,而且很糟糕。
或者也许当会话结束时,您将状态设置为离线?但话又说回来,我该怎么做呢?
有关解决方案的想法和示例会很好。
像 Facebook 这样的网站,在聊天中,如果您离开页面,您会将状态更改为空闲,并且不知何故,如果您离开页面,您会在一段时间内处于离线状态...如何,我不知道。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Facebook 有点令人讨厌,因为他们的页面包含一些与聊天服务器保持开放连接的 JavaScript。一旦连接丢失,这意味着您已关闭页面(或您的互联网连接)并且您被标记为离线。
通常,仅使用超时来将用户在上次活动/页面加载后一段时间标记为离线。例如,此超时的合理值可以是会话 cookie 过期的时间。
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.
简单的解决方案是保存用户最后一次访问的时间,如果他确实注销或者过去的时间太长,则认为他离线。
为了获得更好的解决方案(类似于 facebook),您需要使用半持久连接并使用它们以更精细的粒度检测存在。
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.
好吧,你无法检测到这一点。您能做的最好的事情就是“最后活跃”统计数据或“最后几分钟内活跃的用户”。在这种情况下,您应该更新用户发出页面请求的每种类型的数据库字段。
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.
例如,您可以在用户表中有一个名为last_visit的字段,并在他访问任何页面时使用 sysdate、now() 等更新它...
在为您提供在线用户的查询中,您将使用在 last_visit 字段中停留十分钟或更短时间的用户...
例如:
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:
我想你应该做一些类似于 AJAX 推送的事情。 @Wim 也建议采用类似的方法。
I guess you should do something similar to AJAX push. @Wim suggests a similar approach as well.
我最近一直在用 php 编写一个网站作为业余爱好,改变了规范,我对此的看法是,会话存储在文件中:
last_active
的变量在数据库中的会话ID是不必要的,只是很高兴看到谁有哪个会话ID处于活动状态,我什至编写了一些黑客函数来手动删除基于此ID的会话文件,如果我想注销某人……没有什么真正的原因,只是一点乐趣。
当然,如果您将会话保存在数据库中,那么就更容易了,因为无论如何您都会在每个页面上进行数据库访问,因此更新它并不是什么大问题。
您还可以添加一些 ajax 来保持数据库更新,即使页面打开但没有主动刷新,就像其他人指出的那样,您可以通过检查谁的最后更新数据库条目在您想要的任意范围内来检查谁在线。
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:
last_active
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.