当前位置: 首页 > 图文教程 > 网络编程 > PHP > 粗略计算在线时间,bug:ip相同

PHP
第十三节--对象串行化 -- Classes and Objects in PHP5 [13]
第十一节--重载 -- Classes and Objects in PHP5 [11]
第十节--抽象方法和抽象类 -- Classes and Objects in PHP5 [10]
第九节--绑定 -- Classes and Objects in PHP5 [9]
第八节--访问方式 -- Classes and Objects in PHP5 [8]
第七节--类的静态成员 -- Classes and Objects in PHP5 [7]
第六节--访问属性和方法 -- Classes and Objects in PHP5 [6]
第五节--克隆 -- Classes and Objects in PHP5 [5]
第四节--构造函数和析构函数 -- Classes and Objects in PHP5 [4
第三节--定义一个类 -- Classes and Objects in PHP5 [3]
第二节--PHP5 的对象模型 -- Classes and Objects in PHP5 [2]
第一节--面向对象编程 -- Classes and Objects in PHP5 [1]
初探 PHP5 (二)
初探 PHP5 (一)
SSI使用详解(二)
SSI使用详解(一)
Cookie及其使用(二)
Cookie及其使用(一)
实现跨域名Cookie
两种统计当前在线人数的方法

PHP 中的 粗略计算在线时间,bug:ip相同


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 273 ::
收藏到网摘: n/a

<?PHP
/*
CREATE TABLE `db_online` (
`ip` char(20) default NULL,
`time` char(20) NOT NULL default '',
`name` char(200) NOT NULL default '游客'
) TYPE=MyISAM
*/
//粗略计算在线时间,bug:ip相同(局域网->外部网)者,只记录一人。不过几率很少
session_start();
//超时时间
$out_time=300;//60*5
$uesr_name=$_SESSION['uesr_name'];
$now=time();
$online="db_online";
$ip=$_SERVER["REMOTE_ADDR"];
mysql_connect("localhost","root","");
mysql_select_db("数据库");
//删除过时用户.
mysql_query("delete from `$online` where ($now-`time`)>$out_time or `name`='$uesr_name' or `ip`='$ip' ");
if($uesr_name){
mysql_query(" INSERT INTO `$online` (`ip`, `time`, `name`) VALUES ('$ip','$now','$uesr_name') ");
}else{
mysql_query(" INSERT INTO `$online` (`ip`, `time`, `name`) VALUES ('$ip','$now','游客') ");
}
?>