如果用户退出网站则设置值
这是我遇到的一个问题。当用户登录网站时,它会设置一个值来指示他们处于离线状态。如果他们通过网站注销,该值将设置为指示用户处于离线状态。
但如果用户只是关闭网站而没有按注销,则仍然表明他们在线。
我怎样才能做到这一点,以便在他们关闭网站后使他们离线。
我的网站使用 php、html、css 和 mysql。
heres a issue i have. When a user logs in on the website, it sets a value to indicate they are offline. If they logout through the website, the value is set to indicate the user is offline.
But if the user just closes the website without pressing logout, it still indicates they are online.
How can i make it so it makes them offline once they have closed the website.
my website is using php, html, css and mysql.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最常见的方法是保存用户上次活动的时间戳,而不仅仅是“在线”标志。更新每个活动的时间戳,并通过检查不活动时间超过五分钟的用户来计算离线用户。
出于性能原因,您可能还希望将时间戳保存到用户当前会话中,并且仅在数据库中的活动时间戳即将过期时更新它。
The most common approach is to save a timestamp with the user's last activity instead of just an "online" flag. Update the timestamp on every activity and calculate offline users by checking for users which have been inactive for more than, say, five minutes.
For performance reasons you may want to save the timestamp into the users current session as well and only update your activity timestamp in the database when it is about to expire.
由于关闭浏览器(或浏览器选项卡)不会向您的服务器触发任何事件,因此基本上您无法对此做出反应。在这种情况下,我更喜欢心跳机制。
另一种方法是,如果客户端在 20 分钟左右没有触发任何事件,则“假设”客户端已注销。
此处讨论了类似的问题:检查用户是否离线
Since closing a browser (or a browser tab) doesn't fire any events to your server, basically you can't react to this. In such a case I'd prefer a heartbeat mechanism.
Another way is to "assume" the client has logged out if he hasn't fired any event since lets say 20mins or so.
A similar issue has been discussed here: Check if user is offline
例如,您可以通过 Ajax 检查用户是否正在“应答”。或者您可以通过不活动超时设置离线状态。
You can check for user are "answering" by Ajax for example. Or you can set status offline by inactivity timeout.
也许当浏览器关闭时有一些 javascript 事件,您可以使用 ajax 向服务器发送通知。
我猜想更好的方法是让客户端的 JavaScript 定期通知服务器用户仍然在那里。一旦没有收到通知 - 他一定处于离线状态。
perhaps there is some javascript event when browser closes, on which you could using ajax send notification to the server.
A better approach i would guess is to have client's javascript to periodically notify server that user is still there. Once notification is not received - he must be offline.