也谈php网站在线人数统计

5年以前  |  阅读数:902 次  |  编程语言:PHP 

function checkOnline($userid,$tempid=null)
{
$conn = connect();

//对于所有用户
//先设置自己为在线
$stmt = "UPDATE ".DB_NAME.".USER SET IsOnline='Y' WHERE UserID=".$userid;
$result = query($stmt,$conn);
//info($stmt);
//如果当前用户是游客
if ($tempid != null)
{
$stmt = "SELECT TempID FROM ".DB_NAME.".TEMPUSER WHERE

TempID=".$tempid;
$result = query($stmt,$conn);
//info($stmt);
//如果该游客还在线
if ($row = fetch_array($result))
{
$stmt = "UPDATE ".DB_NAME.".TEMPUSER SET

RequestTime='".getCurrentTime()."' WHERE TempID=".$tempid;
$result = query($stmt,$conn);
//info($stmt);
}
//该游客已经离线
else
{
$stmt = "INSERT INTO ".DB_NAME.".TEMPUSER

VALUES('".$tempid."','".getCurrentTime()."')";
$result = query($stmt,$conn);
//info($stmt);
}
}

//查看其他用户
//普通用户
$stmt = "UPDATE ".DB_NAME.".USER SET IsOnline='N' WHERE ".time()." -

unix_timestamp(RequestTime) > ".ONLINE_DURATION." AND UserGroupID != ".GUEST;
$result = query($stmt,$conn);

//游客
$stmt = "DELETE FROM ".DB_NAME.".TEMPUSER WHERE ".time()." -

unix_timestamp(RequestTime) > ".ONLINE_DURATION;
$result = query($stmt,$conn);
disconnect($conn);
}

//得到在线人数,分用户和游客
function getOnlineNumber()
{
$olnum = array();
$conn = connect();
$stmt = "SELECT COUNT(UserID) FROM ".DB_NAME.".USER WHERE IsOnline='Y' AND

UserGroupID != 4"; //4 为guest的用户组id
//info ($stmt);
$result = query($stmt,$conn);
$olnum['user'] = result($result,0,"COUNT(UserID)");
$stmt = "SELECT COUNT(TempID) FROM ".DB_NAME.".TEMPUSER";
//info ($stmt);
$result = query($stmt,$conn);
if ($row = fetch_array($result))
{
$olnum['guest'] = $row['COUNT(TempID)'];
}
disconnect($conn);
return $olnum; //from www.w3sky.com
}

其中的connect(), disconnect(), query(),fetch_array()函数在dbmanager.inc.PHP中
dbmanager.inc.PHP

define("DB_NAME","databasename");
define("DB_USER","user");
define("DB_PASS","pass");
define("DB_HOST","localhost");

function connect()
{
//echo "Connecting to Host:".HOST."
";
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASS);
mysql_select_db(DB_NAME);
/
if ($conn)
{
echo "Connect to database sucessfully. connection id:".$conn."
";
}
else
{
echo "Connect to database failed.
";
}
/
return $conn;
}

function pconnect()
{
return mysql_pconnect(DB_HOST,DB_USER,DB_PASS);
}

function disconnect($conn)
{
$close = mysql_close($conn);
/
if ($close)
echo "MySQL Database disconnected.
";
else
echo "MySQL Database disconnecting failed. Please try again.
";
/
}

function query($stmt,$conn)
{
return mysql_query($stmt,$conn); //from www.w3sky.com
}

function fetch_array($result)
{
return mysql_fetch_array($result);
}

function fetch_row($result)
{
return mysql_fetch_row($result);
}

function num_rows($result)
{
return mysql_num_rows($result);
}

function result($result,$row,$field)
{
return mysql_result($result,$row,$field);
}

rule层:
rl_online.PHP
function getOnline()
{
if ($userid == 2)
{
if (session_is_registered("tempuserid"))
{
checkOnline($userid,$tempuserid);
}
}
else
{
checkOnline($userid);
}
return getOnlineNumber();
}

ui层:
ui_online.PHP
$online_num = getOnline();
echo "在线人数,注册用户".$online_num['user']."人,游客".$online_num['guest']."人";

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例