<?
include_once("inc/config.php");
include_once("inc/ftbbsdbclass.php");
include_once("inc/publicfun.php");
include_once("conn.php");
$query = "SELECT * FROM {$tablepre}ftbbsmb";
$ftbbsmb=makecache('ftbbsmb.html',$query,$expire);
$onlinetime=$ftbbsmb[40];
//调用方法 如 http://127.0.0.1/bbs/online.php?dis=yes&stats=当前位置,dis参数如果为yes表示要显示在线数，如果没有则不显示，stats为当前的位置
//username值版主为2管理员为1

$timestamp = date("Y-m-d H:i:s");
$sessid = session_id();
$ip = GetUserIP(); 
$stats=ftbbs_request($_GET["stats"]);
$dis=ftbbs_request($_GET["dis"]);
$username=get_cookie('username');
$bz==get_cookie('bz');
$bbsadmin=get_cookie('bbsadmin');

$sql="select * from {$tablepre}ftbbs_online where ip='".$ip."'";
$rs=mysql_query($sql);
//$row = mysql_fetch_array($rs);
$rsnum = mysql_num_rows($rs);

If ($username=="" || empty($username)){ //判断版主或管理员
   $admin=0;
   }
else{
   if ($bz==1){
   $admin=2;
   }
   elseif ($bbsadmin==1){
   $admin=1;
   }
   else{
   $admin=0;
   }
}

if ($rsnum!=0){
    If ($username=="" || empty($username)){
        mysql_query("Update {$tablepre}ftbbs_online set username='匿名',useradmin=0,stats='".$stats."',ip='".$ip."',lasttime='" .$timestamp. "' where ip='".$ip."'");
	  }
	else{
        mysql_query("Update {$tablepre}ftbbs_online set username='".$username."',useradmin='".$admin."',stats='".$stats."',ip='".$ip."',lasttime='" .$timestamp. "' where ip='".$ip."'");
    }
}
else{
    If ($username=="" || empty($username)){
		mysql_query("insert into {$tablepre}ftbbs_online (username,useradmin,stats,ip,logintime,lasttime) values('匿名',0,'$stats','$ip','$timestamp','$timestamp')");
        mysql_query("OPTIMIZE TABLE '{$tablepre}ftbbs_online'");
	  }
	else{
        $sql="select username,lasttime from {$tablepre}ftbbs_online where username='".$username."'";
        $rs=mysql_query($sql);
        //$row = mysql_fetch_array($rs);
        $rsnum = mysql_num_rows($rs);
		if ($rsnum!=0){
		mysql_query("Update {$tablepre}ftbbs_online set lasttime='" .$timestamp. "',stats='".$stats."' where username='" .$username. "'");
		}
		else{
		mysql_query("insert into {$tablepre}ftbbs_online (username,useradmin,stats,ip,logintime,lasttime) values('".$username."',".$admin.",'".$stats."','".$ip."','" .$timestamp. "','" .$timestamp. "')");
		}
    }
}

//删除60分钟前的用户
$sql="select * from {$tablepre}ftbbs_online";
$rs=mysql_query($sql);
$row = mysql_fetch_array($rs);
while($row=mysql_fetch_array($rs)){ 
   $lasttime=$row["lasttime"];
   $minute=dateDiff("MINUTE", $lasttime, $timestamp);
   if($minute>$onlinetime){
      mysql_query("delete from {$tablepre}ftbbs_online"); 
	}  
}

$sql="select * from {$tablepre}ftbbs_online where username='匿名'";
$rs=mysql_query($sql);
$count1 = mysql_num_rows($rs);

$sql="select * from {$tablepre}ftbbs_online where username<>'匿名'";
$rs=mysql_query($sql);
$count2 = mysql_num_rows($rs);
$count=$count1+$count2;

$sql="select * from {$tablepre}ftbbs_online where stats='".$stats."'";
$rs=mysql_query($sql);
$count3 = mysql_num_rows($rs);
mysql_free_result($rs);
$db->close(); 
If ($dis=="yes"){
   echo "document.write('总共<font color=red>$count</font>人在线,其中注册用户<font color=red>$count2</font>人,游客<font color=red>$count1</font>人,当前位置 [$stats] 共有<font color=red>$count3</font>人')";
}
?>