也谈php网站在线人数统计
程序员文章站
2023-08-24 09:37:03
function checkonline($userid,$tempid=null) &nb...
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']."人";
{
$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']."人";