在線人數(shù)統(tǒng)計(jì)源代碼
發(fā)表時(shí)間:2024-02-26 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]根據(jù)我的上篇文章《也談網(wǎng)站在線人數(shù)統(tǒng)計(jì)》,我寫的在線人數(shù)統(tǒng)計(jì)程序源代碼如下:如有錯(cuò)誤請(qǐng)指正。 db層: db_online.php //該函數(shù)使用戶自己在線,并且檢查其他用戶的在線情況 function checkOnline(userid,tempid=null) conn = connect...
根據(jù)我的上篇文章《也談網(wǎng)站在線人數(shù)統(tǒng)計(jì)》,我寫的在線人數(shù)統(tǒng)計(jì)程序源代碼如下:如有錯(cuò)誤請(qǐng)指正。
db層:
db_online.php
//該函數(shù)使用戶自己在線,并且檢查其他用戶的在線情況
function checkOnline($userid,$tempid=null)
{
$conn = connect();
//對(duì)于所有用戶
//先設(shè)置自己為在線
$stmt = "UPDATE ".DB_NAME.".USER SET IsOnline='Y' WHERE UserID=".$userid;
$result = query($stmt,$conn);
//info($stmt);
//如果當(dāng)前用戶是游客
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);
}
//該游客已經(jīng)離線
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);
}
//得到在線人數(shù),分用戶和游客
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()函數(shù)在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 "在線人數(shù),注冊(cè)用戶".$online_num['user']."人,游客".$online_num['guest']."人";