如何知道用户是否仍在网站上在线或离线
可能重复:
检查用户是否离线
我正在建立一个社区网站.用户将照常登录和注销.我使用在线/离线状态属性来设置用户状态.但是,如果用户只是单击X按钮或以其他方式断开连接而没有注销,该怎么办?它显示登录状态.我如何知道用户当前是否在线?
I'm building a community website. Users will login and logout as usually. I use the attribute status online/offline to set the user's status. But what if a user just clicks the X button or disconnects otherwise without logging out? it shows login status.How i get to know user is currently online or not ?
您有三个选择:
-
如果用户在特定时间段(例如10分钟)内未加载新页面,则可以假定该用户处于离线状态.这可以通过在服务器上为每个用户保留最后页面加载时间"来完成.
You can assume the user is offline if they don't load a new page within a specific period of time (e.g. 10 minutes). This would be done by keeping a "last page load time" for each user on the server.
您可以在页面上有一个脚本,该脚本经常触发AJAX调用,以告诉服务器我还在这里"-这将使用与#1相同的逻辑,但会更多可靠.
You can have a script on your pages that fires off an AJAX call every so often to tell the server "I'm still here" - this would use the same logic as #1, but would be somewhat more reliable.
您可以使用页面卸载AJAX调用来告诉服务器浏览器何时卸载页面,并将用户标记为脱机,直到他们加载另一个页面.
You can use a page-unload AJAX call to tell the server when the browser is unloading the page, and mark the user as offline until they load another page.