如何在php中检测用户是否已注销? [英] How to detect if a user has logged out, in php?

查看:91
本文介绍了如何在php中检测用户是否已注销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户成功登录后,我将login = true存储在数据库中.但是,如何在不单击注销按钮的情况下关闭浏览器来注销用户?另外,如何将闲置10分钟的用户重定向到登录页面?

After the user successfully logs in, I store login = true in database. But how do I check if the user logged out by closing the browser without clicking the logout button? And also, how do I redirect user who has been inactive for 10 minutes to login page?

我正在使用php和mysql.任何帮助将不胜感激.

I am using php and mysql. Any help would be appreciated.

对不起,如果我的问题不清楚.我确实使用会话来存储它们是否已登录.但是,现在我想将信息存储在数据库中,以便可以在其他页面上显示它们的状态.假设user1有3个朋友.当显示他的所有朋友时,user1想知道他的朋友是在线还是离线.这就是我要的.有什么建议吗?

Sorry if my question is not clear. I did use session to store whether they are logged-in or not. But, now I want to store the info in database, so that I can display their status on other pages. Let's say user1 has 3 friends. When displaying all his friends, user1 want to know whether his friends are online or offline. This is what I want. Any advise?

推荐答案

2017这些天,最好的选择是使用网络套接字来跟踪页面/站点上的状态.

2017 edit: These days, your best bet is using websockets to track presence on a page/site.

您无法检测到用户何时关闭浏览器或使用PHP导航离开您的站点,而这样做的JavaScript技术远不能保证没有用.

You cannot detect when a user closes their browser or navigates off your site with PHP, and the JavaScript techniques of doing so are so far from guaranteed as to be useless.

相反,最好的选择是最有可能存储每个用户的上次活动时间.

Instead, your best bet is most likely to store each user's last activity time.

  • 在用户表中沿"last_activity"行创建一列.
  • 每当用户加载页面时,请将其last_activity更新为当前时间.
  • 要获取谁在线的列表,只需在数据库中查询具有last_activity值(而不是10/20/分钟)的最近用户.

这篇关于如何在php中检测用户是否已注销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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