也谈php网站在线人数统计

前端之家收集整理的这篇文章主要介绍了也谈php网站在线人数统计前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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,"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."
";
$conn=MysqL_connect(DB_HOST,DB_USER,DB_PASS);
MysqL_select_db(DB_NAME);
/
if($conn)
{
echo"Connecttodatabasesucessfully.connectionid:".$conn."
";
}
else
{
echo"ConnecttodatabaseFailed.
";
}
/
return$conn;
} functionpconnect()
{
returnMysqL_pconnect(DB_HOST,DB_PASS);
} functiondisconnect($conn)
{
$close=MysqL_close($conn);
/
if($close)
echo"MysqLDatabasedisconnected.
";
else
echo"MysqLDatabasedisconnectingFailed.Pleasetryagain.
";
/
} 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,$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']."人";

原文链接:https://www.f2er.com/php/29693.html

猜你在找的PHP相关文章