如果用户退出网站则设置值

发布于 2024-11-19 14:48:03 字数 184 浏览 0 评论 0原文

这是我遇到的一个问题。当用户登录网站时,它会设置一个值来指示他们处于离线状态。如果他们通过网站注销,该值将设置为指示用户处于离线状态。

但如果用户只是关闭网站而没有按注销,则仍然表明他们在线。

我怎样才能做到这一点,以便在他们关闭网站后使他们离线。

我的网站使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

留蓝 2024-11-26 14:48:03

最常见的方法是保存用户上次活动的时间戳,而不仅仅是“在线”标志。更新每个活动的时间戳,并通过检查不活动时间超过五分钟的用户来计算离线用户。

出于性能原因,您可能还希望将时间戳保存到用户当前会话中,并且仅在数据库中的活动时间戳即将过期时更新它。

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.

贩梦商人 2024-11-26 14:48:03

由于关闭浏览器(或浏览器选项卡)不会向您的服务器触发任何事件,因此基本上您无法对此做出反应。在这种情况下,我更喜欢心跳机制。

另一种方法是,如果客户端在 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

梦断已成空 2024-11-26 14:48:03

例如,您可以通过 Ajax 检查用户是否正在“应答”。或者您可以通过不活动超时设置离线状态。

You can check for user are "answering" by Ajax for example. Or you can set status offline by inactivity timeout.

浅暮の光 2024-11-26 14:48:03

也许当浏览器关闭时有一些 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文