简体   繁体   English

如何找出创建的会话总数,即已登录的用户数?

[英]How do I find out total number of sessions created i.e. number of logged in users?

Hi guys I have a simple membership based website where users log in and have their own profiles. 嗨,大家好,我有一个简单的基于成员资格的网站,用户可以登录并拥有自己的个人资料。 I woudl like to be able to tell at any given point in time how many users or rather which users are currently logged into my website. 我希望能够在任何给定的时间点告诉我当前有多少用户或哪些用户登录了我的网站。

Its a simple membership based system in php. 它是一个基于php的简单的基于成员资格的系统。 One way I uderstand it creating a table in a db and storing login details there and session data as well but can this be done in some other way ie anyway to find out how many sessions have been started and all the unique users online. 我了解它的一种方式是在数据库中创建一个表,并在那里存储登录详细信息和会话数据,但是可以通过其他方式完成,即无论如何找出已经启动了多少会话以及所有唯一用户在线。

The best way to do this is record the lastActivityTime for each user. 最好的方法是记录每个用户的lastActivityTime When they access a page, log the time in their db-record. 当他们访问页面时,将时间记录在他们的数据库记录中。 Once you're doing that, run a simple query to count all records that have a lastActivityTime less than 5 minutes from the Current Time . 完成此操作后,运行一个简单的查询以计算lastActivityTime距离Current Time少于5分钟的所有记录。

So rather than asking "how many users are logged in," you're asking "how many users were recently active." 因此,您不是在询问“有多少用户登录”,而是在询问“最近有多少用户处于活动状态”。

The key problem with the whole 'monitoring active sessions' issue is that you (the server) are not necessarily in full control of the session. 整个“监视活动会话”问题的关键问题是您(服务器)不一定完全控制会话。 User's browsers can expire the sessions at any given time, so you cannot guarantee active users at any point. 用户的浏览器可以在任何给定时间使会话期满,因此您不能保证任何时候的活动用户。

If it is extremely important to know the current users, then add a column to their user table specifying a timestamp, and update that field every time they load a page. 如果了解当前用户非常重要,请在其用户表中添加一列以指定时间戳,并在每次加载页面时更新该字段。 If that column is less than about a minute out of date, then they can be considered active. 如果该列已过时不到一分钟,则可以认为它们是活动的。

An extra thing you can do (used a lot by online chatrooms to maintain an active 'chat' list), is to poll a page at a given interval (using AJAX), and then even if they don't refresh the page you still know they are there. 您还可以做一件事(在线聊天室经常使用它来维护活动的“聊天”列表),以给定的时间间隔(使用AJAX)轮询页面,即使他们没有刷新页面,您仍然知道他们在那里。 If an ajax request doesn't arrive at the specified interval, the user has navigated away, or shut their browser. 如果ajax请求没有在指定的时间间隔内到达,则表明用户已经离开或关闭了浏览器。

One method I've used is to write a custom session handler - there are plenty of examples on php.net. 我使用的一种方法是编写自定义会话处理程序-php.net上有很多示例。 Once the session data is in SQL there's a lot more you can do with it. 一旦会话数据存储在SQL中,您可以做更多的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算会话以显示站点上已登录用户的总数 - How to count SESSIONS to display total number of logged in users on the site 如何显示Wordpress页码和总页数(即第1页,共3页) - How to show Wordpress page number and total number of pages (i.e., page 1 of 3) 如何使用会话使我简单的php / html登录安全? 即注销后无法“返回”应用程序 - How to make my simple php/html login secure using Sessions? i.e. Unable to 'back' into application when logged out 如何为已登录和已注销的用户构建文件夹? - How do I structure folders for users logged in and logged out? 如何为可变参数函数编写Doxygen注释,即具有未定义数量的参数的函数? - How to write a Doxygen comment for variadic function, i.e., a function with undefined number of arguments? 如何将$ cart_total更改为实际数字? - How do I change $cart_total to an actual number? 如何获得仅活跃的学生总数 - How do I get the total number of students who are only active PDO如何返回已执行查询的总数 - PDO how do I returns the total number of executed queries WordPress-如何显示条目的评论总数? - WordPress - How do I display the total number of comments from an entry? 如何跟踪已登录用户的活动? - How do I track the activity of logged in users?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM