也谈php网站在线人数统计
2015-01-24信息快讯网
{
$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']."人";
php+memcache实现的网站在线人数统计代码
PHP提取数据库内容中的图片地址并循环输出
PHP中10个不常见却非常有用的函数
初次接触php抽象工厂模式(Elgg)
PHP5与MySQL数据库操作常用代码 收集
ajax+php打造进度条 readyState各状态
PHP 在线翻译函数代码
wiki-shan写的php在线加密的解密程序
php在线打包程序源码
php 在线打包_支持子目录
php下使用以下代码连接并测试
php实现的在线人员函数库
PHP循环获取GET和POST值的代码
生成卡号php代码
PHP获取网卡地址的代码
PHP防注入安全代码
可以在线执行PHP代码包装修正版
php在线生成ico文件的代码
站长助手-网站web在线管理程序 v1.0 下载
手把手教你使用DedeCms V3的在线采集图文教程
基于数据库的在线人数,日访问量等统计
网站当前的在线人数