也談php網站線上人數統計

來源:互聯網
上載者:User

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."<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); //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']."人";

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.