<?php
/*
////////////////
//统计在线人数//
////////////////
*/
$u_time  = time();                   //操作时间
$u_name  = $id;                      //用户名
$u_url   = $_SERVER['PHP_SELF'];     //操作地址
function user_ip(){
      
if ($_SERVER["HTTP_X_FORWARDED_FOR"]) 
{ 
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; 
} 
elseif ($_SERVER["HTTP_CLIENT_IP"]) 
{ 
$ip = $_SERVER["HTTP_CLIENT_IP"]; 
}
elseif ($_SERVER["REMOTE_ADDR"]) 
{ 
$ip = $_SERVER["REMOTE_ADDR"]; 
} 
elseif (getenv("HTTP_X_FORWARDED_FOR")) 
{ 
$ip = getenv("HTTP_X_FORWARDED_FOR"); 
} 
elseif (getenv("HTTP_CLIENT_IP")) 
{ 
$ip = getenv("HTTP_CLIENT_IP"); 
} 
elseif (getenv("REMOTE_ADDR"))
{ 
$ip = getenv("REMOTE_ADDR"); 
} 
else 
{ 
$ip = "Unknown"; 
} 
    return $ip;
	  }
	  $u_ip    = user_ip();  //客户端IP
function online($time,$name,$wapid,$ip,$url,$saved,$saved_time,$online_time,$type){
    if($saved=='TRUE'){
    //首先删除数据库中的过时的记录
	$limitsaved = $time - $saved_time;
	 mysql_query("DELETE FROM user_online WHERE user_time<'{$limitsaved}'");
	 }else{
	 $limitsaved = $time - $online_time;
	 mysql_query("DELETE FROM user_online WHERE user_time<'{$limitsaved}'");
	 }
	//如果存在相同IP则删除旧的IP操作记录
	if($ip!=NULL && $ip!=''){
	    mysql_query("DELETE FROM user_online WHERE user_ip='{$ip}' AND user_name='{$name}'");
	}
    //添加访问记录至数据库
	$sql="INSERT INTO user_online(user_name,user_url,user_ip,siteid,user_time)VALUES('{$name}','{$url}','{$ip}','{$wapid}','{$time}')";
	//执行数据库语句
	    mysql_query($sql);
    if($type=='wap_all'){
    //查询全站在线记录
	$limits = $time - $online_time;
	$sql2 = "SELECT count(*) FROM user_online WHERE user_time>'{$limits}' AND siteid='{$wapid}'";   
	$online = mysql_query($sql2); 
	list($lnums) = mysql_fetch_row($online);
	if($lnums==NULL){
	$lnums='0';
	       }
	 }elseif($type=='wap_all_user'){
	 //查询全站注册用户在线记录
	$limits = $time - $online_time;
	$sql2 = "SELECT count(*) FROM user_online WHERE user_time>'{$limits}' AND siteid='{$wapid}' AND user_name!=NULL";   
	$online = mysql_query($sql2); 
	list($lnums) = mysql_fetch_row($online);
	if($lnums==NULL){
	$lnums='0';
	    }
   }
   return $lnums;
}
//实例化全站在线人数
$user_online_wapall = online($u_time,$u_name,$wapid,$u_ip,$u_url,$cfg['ol_saved'],$cfg['saved_time'],$cfg['ol_time'],'wap_all');
//实例化全站注册会员在线人数
$user_online_wapalluser = online($u_time,$u_name,$wapid,$u_ip,$u_url,$cfg['ol_saved'],$cfg['saved_time'],$cfg['ol_time'],'wap_all_user');
//全站非注册用户在线人数
$user_online_wapnologin = $user_online_wapall-$user_online_wapalluser;
////////////////////
//检测用户是否在线//
////////////////////
$olt=$cfg['ol_time'];
function u_online($id,$wapid){
    //检查操作记录中是否存在用户的信息
	$limits = time() - $GLOBALS['olt'];
    $sql= "SELECT user_time,user_name,user_ip,user_url FROM user_online WHERE user_time>'{$limits}' AND siteid='{$wapid}' AND user_name='{$id}'";   
    $online=mysql_query($sql);
	if($online && mysql_num_rows($online)>0){
	  $back = 1;
	  }else{
	  $back = 2;
	}
    return $back;
}


?>