用户关闭浏览器时如何处理在线状态? [英] How to handle user online status when he/she close the browser?

查看:104
本文介绍了用户关闭浏览器时如何处理在线状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,可以通过将"is_online"字段设置为1来跟踪用户的在线状态.现在,用户登录该站点并关闭其系统,或者在登录时断电. 在数据库"is_login"中,该值的值为"1",表示他仍在使用该网站.

I am having table to track user online status by setting 'is_online' field to 1. Now User get logged in the site and shut down his/her system or power down at that time he/she logged in. Here in database 'is_login' has the value '1' which means he is still using the site.

所以我必须做一些逻辑才能将该值更改为"0".

So I have to do some logic to change that value to '0'.

我如何使用PHP处理这种情况?

How can i handle this situation using PHP ?

推荐答案

通常的解决方案是将时间戳存储在表中,每次用户执行某项操作时便会对其进行更新.具有最近时间戳记(例如,五分钟)的用户显示为已登录,其他所有人都注销了.

The normal solution is to store a timestamp in the table which you update every time the user does something. Users with a recent timestamp (say, five minutes) are shown as logged in, everybody else are logged out.

它甚至不必在每次加载页面时都进行更新.还要在会话中保留一个时间戳记以及最后一次更新时间,并且仅在数据库标志即将到期时才更新表.

It doesn't even have to be updated on every page load. Keep a timestamp in the session with the last update time as well, and only update the table when the database flag are about to expire.

这篇关于用户关闭浏览器时如何处理在线状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆