Not sure if applicable to your situation at all, but I got this code off the net to use with PHP and MySQL to count, within the last minute, all unique users on a website. Maybe it can give you an idea or you can adapt it.
First, the MySQL table required:
Code:
CREATE TABLE `poweralert3_users_online` (
`timestamp` int(15) NOT NULL default '0',
`ip` varchar(40) NOT NULL default '0',
PRIMARY KEY (`ip`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
and then this code is used to determine the count, and other code (not shown here) displays at the bottom of the page "nnn users online" where nnn is the number of unique IPs:
Code:
function getUsersOnline() {
$server = $_SESSION['hostname'];
$database = $_SESSION['db_name'];
$user = $_SESSION['username'];
$pw = $_SESSION['db_password'];
// connect to the database
$connection = @mysql_connect ($server, $user, $pw) or die ("Error connecting to the database.");
// select the database
$db = @mysql_select_db ($database) or die ("Error selecting database.");
// time limit for users online in seconds - you can increase or decrease this if you want
// recommended: entries in db older than 1 minute will be removed
$timeout = 60;
// get the ip address
$ip = getenv("REMOTE_ADDR");
// get the time
$timestamp = time();
$timelimit = $timestamp - $timeout;
// delete expired users
$sql = "DELETE FROM " . $_SESSION['cms_db_prefix'] ."users_online WHERE timestamp < $timelimit";
$result = @mysql_query ($sql);
// insert current user
$sql = "INSERT INTO " . $_SESSION['cms_db_prefix'] ."users_online (timestamp, ip) VALUES ('$timestamp', '$ip')";
$result = mysql_query ($sql);
// if ip already exists, update the timestamp
if (mysql_errno() == 1062) {
$sql = "UPDATE " . $_SESSION['cms_db_prefix'] ."users_online SET timestamp = '$timestamp' WHERE ip = '$ip'";
$result = mysql_query ($sql);
}
// get number of users online
$sql = "SELECT COUNT(*) as num_users FROM " . $_SESSION['cms_db_prefix'] ."users_online";
$result = mysql_query ($sql);
$row = mysql_fetch_array($result);
$num_users = $row['num_users'];
if ($num_users == 1)
return "1 user";
else
return $num_users . " users";
}
Hope this helps or at least gives you an idea of how you might approach the problem in your environment.