当前位置:首页 > PHP教程 > PHP高级教程

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

<?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','游客')  ");
}

?>

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!