当前位置:Linux教程 - Php - php在线统计

php在线统计

<?php
/*
#
# 表的结构 `usersonline`
#

CREATE TABLE `usersonline` (
`timestamp` int(15) NOT NULL default '0',
`snum` varchar(40) NOT NULL default '',
`type` varchar(5) NOT NULL default '',
`username` varchar(80) NOT NULL default 'guest',
KEY `timestamp` (`timestamp`),
KEY `snum` (`snum`)
) TYPE=MyISAM;

*/

include "function/config.php"; //连接数据库的配置文件

$timeoutSeconds = 140;
$numberOfUsers = 0;







$currentTime = time();
$timeout = $currentTime - $timeoutSeconds;


if(
$PHPSESSID)
{

if(
$username)
{
$type="m";
mysql_query("DELETE FROM usersonline WHERE snum='$PHPSESSID'",$main_link)
or die(
'0 Error deleting from database');
}
else
{
$type="g";
$username="guest";

}

$string=mysql_query("INSERT INTO usersonline VALUES ('$currentTime','$PHPSESSID','$type','$username')",$main_link)
or die(
'Error writing to database');








}
else
{



}

mysql_query("DELETE FROM usersonline WHERE timestamp < $timeout",$main_link)
or die(
'2 Error deleting from database');

$result = mysql_query("SELECT DISTINCT snum FROM usersonline",$main_link)
or die(
'0 Error reading from database');

$this->numberOfUsers = mysql_num_rows($result);


$resulter = mysql_query("SELECT DISTINCT snum FROM usersonline where type='g'",$main_link)
or die(
'1 Error reading from database');
$this->guests=mysql_num_rows($resulter);



$resultest = mysql_query("SELECT DISTINCT snum FROM usersonline where type='m'",$main_link)
or die(
'2 Error reading from database');
$this->members=mysql_num_rows($resultest);

mysql_close();






echo
"目前总共有 $this->numberOfUsers 人在线。其中注册用户 $this->members 人,访客 $this->guests 人。";






?>