首页 > 开发 > PHP > 正文

在线人数统计源代码

2024-05-04 22:56:58
字体:
来源:转载
供稿:网友
根据我的上篇文章《也谈网站在线人数统计》,我写的在线人数统计程序源代码如下:如有错误请指正。

db层:
db_online.php

//该函数使用户自己在线,并且检查其他用户的在线情况
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;
}



其中的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);
}

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']."人";
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表