简体   繁体   English

找出有多少用户在线使用PHP?

[英]Find out how many users are online in PHP?

Every visit to my website updates a user's individual hit counter and updates a column for time() based on their ip address and id stored in a cookie. 每次访问我的网站都会更新用户的单个点击计数器,并根据存储在Cookie中的IP地址和ID更新time()列。 So when coming to output the data, what's a more efficient way of my following code with less database calls, as it's essentially a copy of itself: 因此,在输出数据时,以下代码是一种更有效的方式,它减少了数据库调用,因为它本质上是其自身的副本:

<?
$last1Min = time()-60;
$last5Mins = time()-300;
$last1Hr = time()-6000;
$last1Dy = time()-144000;
$last1Wk = time()-1008000;
$last1Mnth = time()-30240000;

//last1Min
$sql = "SELECT COUNT(*) FROM usersonline WHERE lastOnline > $last1Min";
while($rows = mysql_fetch_array(mysql_query($sql))) {
    echo "Users online in the last minute: " . $rows['COUNT(*)'] . "<br />\n";
}

//last5Mins
$sql = "SELECT COUNT(*) FROM usersonline WHERE lastOnline > $last5Mins";
while($rows = mysql_fetch_array(mysql_query($sql))) {
    echo "Users online in the last 5 minutes: " . $rows['COUNT(*)'] . "<br />\n";
}

//last1Hr
$sql = "SELECT COUNT(*) FROM usersonline WHERE lastOnline > $last1Hr";
while($rows = mysql_fetch_array(mysql_query($sql))) {
    echo "Users online in the last hour: " . $rows['COUNT(*)'] . "<br />\n";
}

//last1Dy
$sql = "SELECT COUNT(*) FROM usersonline WHERE lastOnline > $last1Dy";
while($rows = mysql_fetch_array(mysql_query($sql))) {
    echo "Users online in the last day: " . $rows['COUNT(*)'] . "<br />\n";
}

//last1Wk
$sql = "SELECT COUNT(*) FROM usersonline WHERE lastOnline > $last1Wk";
while($rows = mysql_fetch_array(mysql_query($sql))) {
    echo "Users online in the last week: " . $rows['COUNT(*)'] . "<br />\n";
}

//last1Mnth
$sql = "SELECT COUNT(*) FROM usersonline WHERE lastOnline > $last1Mnth";
while($rows = mysql_fetch_array(mysql_query($sql))) {
    echo "Users online in the last month: " . $rows['COUNT(*)'] . "<br /><br />\n";
}

If there is a more efficient way of presenting this data, I'm wanting to extend it to show not only how many users for each of these metrics is online on my entire site, but record and output the data for every page on my site. 如果有一种更有效的方式来显示此数据,我想对其进行扩展以不仅显示整个站点上每个指标的在线用户数,还记录并输出站点上每个页面的数据。

SELECT 
  SUM(lastOnline <= 60) AS one_minute,
  SUM(lastOnline <= 300) AS five_minutes,
  ...
  SUM(lastOnline <= 30240000) AS one_month
FROM usersonline

Using this method, you can get everything you need in a single query with a single table scan; 使用此方法,您可以通过单个表扫描在单个查询中获得所需的一切。 it doesn't get much more efficient than that. 它没有比这更有效的方法了。 As others have mentioned, you should cache the result, as it's relatively expensive (even in this optimized form). 正如其他人提到的那样,您应该缓存结果,因为它相对昂贵(即使采用这种优化形式)。 There's no point in calculating this on every page load, especially if you're seeing multiple hits per second (which is extremely likely if you, say, hit the front page of digg) 在每次页面加载时都没有必要计算此值,尤其是如果您每秒看到多次点击(如果您点击了digg的首页,则极有可能)

lastOnline <= 60 evaluates to 1 for rows where the condition is true, and 0 for rows where the condition is false; 对于条件为真的行,lastOnline <= 60的结果为1;对于条件为假的行,lastOnline的结果为0; SUM() sums these 1s and zeros, giving you a count of the number of rows for which the condition is true. SUM()将这些1和0相加,得出条件为真的行数。

Learned this technique from a user comment in the mysql docs a few years ago; 几年前从mysql文档中的用户评论中学到了这项技术; there are similar examples elsewhere 其他地方也有类似的例子

Set up a cron job that calculates the correct values only once every minute/5 minutes/etcetera. 设置一个cron作业,该作业每分钟/ 5分钟/等仅计算一次正确的值。 Cache the result and display that instead. 缓存结果并显示出来。 There's really no need to calculate these kinds of stats X times a second when they only change once a minute or once ever half hour. 当这些统计信息每分钟更改一次或每半小时更改一次时,实际上并不需要每秒计算X次。

Instead of calling the database for each of the times you could just make a call for all within the last month and order them by date. 不必每次都调用数据库,您只需在上个月内调用所有数据库,然后按日期对它们进行排序即可。 Then in php you can compare the date and time to see how long ago the user last logged in. 然后在php中,您可以比较日期和时间,以查看用户上次登录的时间。

If you save your sessions to files, you could count the number of files that have been accessed in a time period. 如果将会话保存到文件,则可以计算一个时间段内已访问的文件数。 Then there'd be no database access at all. 这样就根本没有数据库访问权限。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM