设为首页收藏本站

新微赢技术网

 找回密码
 注册
搜索
热搜: 回贴
查看: 9985|回复: 1
打印 上一主题 下一主题

在线人数统计源代码

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-28 01:56:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
//该函数使用户自己在线,并且检查其他用户的在线情况
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;
}



其中的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."<br>";
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASS);
mysql_select_db(DB_NAME);
/*
if ($conn)
{
echo "Connect to database sucessfully. connection id:".$conn."<br>";
}
else
{
echo "Connect to database failed.<br>";
}
*/
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.<br>";
else
echo "MySQL Database disconnecting failed. Please try again.<br>";
*/
}

function query($stmt,$conn)
{
return mysql_query($stmt,$conn);
}

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']."人";
您需要登录后才可以回帖 登录 | 注册

本版积分规则

申请友链|小黑屋|最新主题|手机版|新微赢技术网 ( 苏ICP备08020429号 )  

GMT+8, 2024-11-18 16:36 , Processed in 0.140271 second(s), 9 queries , Gzip On, Memcache On.

Powered by xuexi

© 2001-2013 HaiAn.Com.Cn Inc. 寰耽

快速回复 返回顶部 返回列表