Home >Backend Development >PHP Tutorial >PHP
function checkOnline($userid,$tempid=null)
{
$conn = connect();
//瀵逛簬鎵€链夌敤鎴?nbsp;鍏堣缃嚜chen becomes negative 鍦ㄧHowl
$stmt = "UPDATE ".DB_NAME.".USER SET IsOnline='Y' WHERE UserID=".$userid; > queries(ult= stmt,$conn);
//info($stmt);
//濡傛灉褰按墠鐢ㄦ埛鏄父瀹?nbsp;
if ($tempid {
$stmt = "SELECT TempID FROM ".DB_NAME.".TEMPUSER WHERE
TempID=".$tempid;
$result ($nt =); 🎜>//info($stmt);
//濡傛灉璇ユ父瀹㈣修鍦ㄧ嚎
if ($row = fetch_array($result)) $
{ = "UPDATE ".DB_NAME.".TEMPUSER SET
RequestTime='".getCurrentTime()."' WHERE TempID=".$tempid;
$result = query($stmt,$cont. ;
//info($stmt);
}
//璇ユ父瀹㈠凡缁忕绾?nbsp;
else > $ IN { 🎜= INTO ".DB_NAME.".TEMPUSER
VALUES('".$tempid."','".getCurrentTime()."')";
$result = query($stmt,$conn );
//info($stmt);
}
}
// 鏌ョ湅鍏朵粬鐢ㄦ埛🛱? nbsp;
$stmt = "UPDATE ".DB_NAME.".USER SET IsOnline='N' WHERE ".time()." -
unix_timestamp(RequestTime) ."LINE_D. " UserGroupID != ".GUEST;
$result = query($stmt,$conn);
// 娓稿
$stmt = "DELETE FROM . ".DBEUSNAME " ".time()." -
unix_timestamp(RequestTime) > ".ONLINE_DURATION;
$result = query($stmt,$conn);
disconnect($con >}
//寰楀埌鍦ㄧ嚎浜烘暟锛屽垎鐢ㄦ埛鍜屾父瀹?nbsp;
function getOnlineNumber() > { 🎎 = ray ();
$conn = connect();
$stmt = "SELECT COUNT(UserID) FROM ".DB_NAME.".USER WHERE IsOnline='Y' AND
!U4>
//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
}
鍏朵腑鐨刢onnect(), disconnect(), query(),fetch_array()鍑芥暟鍦╠bmanager.inc.PHP涓?nbsp;
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."
";
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASS);
mysql_select_db(DB_NAME);
/*
if ($conn)
{
echo "Connect to database sucessfully. connection id:".$conn."
";
}
else
{
echo "Connect to database failed.
";
}
*/
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.
";
else
echo "MySQL Database disconnecting failed. Please try again.
";
*/
}
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']."浜?;