functioncheckOnline($userid,$tempid=null)
{
$conn=connect();
//对于所有用户
//先设置自己为在线
$stmt="UPDATE".DB_NAME.".USERSETIsOnline='Y'WHEREUserID=".$userid;
$result=query($stmt,$conn);
//info($stmt);
//如果当前用户是游客
if($tempid!=null)
{
$stmt="SELECTTempIDFROM".DB_NAME.".TEMPUSERWHERE
TempID=".$tempid;
$result=query($stmt,$conn);
//info($stmt);
//如果该游客还在线
if($row=fetch_array($result))
{
$stmt="UPDATE".DB_NAME.".TEMPUSERSET
RequestTime='".getCurrentTime()."'WHERETempID=".$tempid;
$result=query($stmt,$conn);
//info($stmt);
}
//该游客已经离线
else
{
$stmt="INSERTINTO".DB_NAME.".TEMPUSER
VALUES('".$tempid."','".getCurrentTime()."')";
$result=query($stmt,$conn);
//info($stmt);
}
}
//查看其他用户
//普通用户
$stmt="UPDATE".DB_NAME.".USERSETIsOnline='N'WHERE".time()."-
unix_timestamp(RequestTime)>".ONLINE_DURATION."ANDUserGroupID!=".GUEST;
$result=query($stmt,$conn);
//游客
$stmt="DELETEFROM".DB_NAME.".TEMPUSERWHERE".time()."-
unix_timestamp(RequestTime)>".ONLINE_DURATION;
$result=query($stmt,$conn);
disconnect($conn);
}
//得到在线人数,分用户和游客
functiongetOnlineNumber()
{
$olnum=array();
$conn=connect();
$stmt="SELECTCOUNT(UserID)FROM".DB_NAME.".USERWHEREIsOnline='Y'AND
UserGroupID!=4";//4为guest的用户组id
//info($stmt);
$result=query($stmt,$conn);
$olnum['user']=result($result,0,"COUNT(UserID)");
$stmt="SELECTCOUNT(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;//fromwww.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");
functionconnect()
{
//echo"ConnectingtoHost:".HOST."<br>";
$conn=mysql_connect(DB_HOST,DB_USER,DB_PASS);
mysql_select_db(DB_NAME);
/*
if($conn)
{
echo"Connecttodatabasesucessfully.connectionid:".$conn."<br>";
}
else
{
echo"Connecttodatabasefailed.<br>";
}
*/
return$conn;
}
functionpconnect()
{
returnmysql_pconnect(DB_HOST,DB_USER,DB_PASS);
}
functiondisconnect($conn)
{
$close=mysql_close($conn);
/*
if($close)
echo"MySQLDatabasedisconnected.<br>";
else
echo"MySQLDatabasedisconnectingfailed.Pleasetryagain.<br>";
*/
}
functionquery($stmt,$conn)
{
returnmysql_query($stmt,$conn);//fromwww.w3sky.com
}
functionfetch_array($result)
{
returnmysql_fetch_array($result);
}
functionfetch_row($result)
{
returnmysql_fetch_row($result);
}
functionnum_rows($result)
{
returnmysql_num_rows($result);
}
functionresult($result,$row,$field)
{
returnmysql_result($result,$row,$field);
}
rule层:
rl_online.PHP
functiongetOnline()
{
if($userid==2)
{
if(session_is_registered("tempuserid"))
{
checkOnline($userid,$tempuserid);
}
}
else
{
checkOnline($userid);
}
returngetOnlineNumber();
}
ui层:
ui_online.PHP
$online_num=getOnline();
echo"在线人数,注册用户".$online_num['user']."人,游客".$online_num['guest']."人";