hi guys, here is my solution for counting active users. of course it will be inacurate if multiple users come from one ip - for my purposes that is not an issue.
it is pretty simple - it logs every ip and the time of their last load. then to display active users it selects * where last load was within 5 mins.
and the source:
<?
/*
Paul Gareau - paul@xhawk.net
Copyright (C) 2001 - Paul Gareau
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
class users
{
var $interval;
function users()
{
$this->interval = 5; //minutes
$this->page_load();
}
function page_load()
{
global $link_id;
global $REMOTE_ADDR;
$sql = "SELECT user_ip
FROM users
WHERE user_ip='$REMOTE_ADDR'";
$result = mysql_query($sql, $link_id);
$num_rows = mysql_num_rows($result);
if($num_rows)
{
$sql = "UPDATE users SET time=NOW()
WHERE user_ip='$REMOTE_ADDR'";
$result = mysql_query($sql, $link_id);
}
else
{
$sql = "INSERT INTO users(user_ip, time)
VALUES('$REMOTE_ADDR', NOW())";
$result = mysql_query($sql, $link_id);
}
}
function active_users()
{
global $link_id;
$sql = "SELECT count(user_ip) as num_users
FROM users
WHERE time > DATE_SUB(NOW(), INTERVAL $this->interval MINUTE)";
$result = mysql_query($sql, $link_id);
$record = mysql_fetch_object($result);
return $record->num_users;
}
function total_users()
{
global $link_id;
$sql = "SELECT count(user_ip) as total_users
FROM users";
$result = mysql_query($sql, $link_id);
$record = mysql_fetch_object($result);
return $record->total_users;
}
}
?>
this assumes you have a db connection to $link_id established.
youll need to make a table named users with columns user_ip and time. good luck!
-paul
http://xhawk.net