<?php 
/*
////////////////
//统计在线人数//
////////////////
*/
$u_time  = time();                   //操作时间
$u_name  = $id;                      //用户名
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;
	}
	
	//今天0时0分的时间戳
	$todayTime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	
	//本月1日0时0分时间戳
	$monthTime = mktime(0,0,0,date('m'),1,date('Y'));
	
	//本年1月1日时间戳
	$yearTime = mktime(0,0,0,1,1,date('Y'));
	
	define("T_TIME",$todayTime);
	
	define("M_TIME",$monthTime);
	
	define("Y_TIME",$yearTime);
	
	$u_ip    = user_ip();  //客户端IP
	
function v_logs($saved,$saved_time){
	
    if($saved == 'TRUE' || $saved == TRUE){
   
	if($saved_time < 60*60*24){
	/*检查保存时间是否小于24小时*/
		$saved_time = 60*60*24;
	}
	
	$limitsaved = $time - $saved_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 usertime >='".$todayTime."'");
	}*/
   
	//添加访问记录至数据库
	mysql_query("INSERT INTO user_online(user_name,user_url,user_ip,siteid,user_time)VALUES('".MYID."','{$_SERVER['PHP_SELF']}','".user_ip()."','".WAPID."','".time()."')");
	
	}else{
	/*如果不进行统计则清空数据*/
	mysql_query("DELETE FROM user_online WHERE user_time >= '0'");
	}
	/*将统计数据分类汇总到访问量数据表*/
	put_logs();
}
//对当天的IP进行分类成立到三维数组中
function count_ip(){
		$sql= "SELECT user_time,user_name,user_ip,user_url FROM user_online WHERE user_time>'".T_TIME."'";   
		$online = mysql_query($sql);
		if($online && mysql_num_rows($online)>0){
			while(list($user_time,$user_name,$user_ip,$user_url) = mysql_fetch_row($online)){
				$count_ip[$user_ip][] = $user_url;
			}
		}
	return $count_ip; 
}
function yday($str){
		$sql= 'SELECT '.$str.',visits_date,visits_id FROM visits WHERE visits_date<\''.T_TIME.'\' ORDER BY visits_id DESC LIMIT 0,1';   
		$online = mysql_query($sql);
		if($online && mysql_num_rows($online)>0){
			while(list($strb,$idb,$visits_id) = mysql_fetch_row($online)){
				return $strb;
			}
		}
	 
}
function today_ip(){
	$nums = count(count_ip());
	if($nums == NULL){
		$nums = 0;
	}
	return $nums;//返回今日iP访问量
}
function today_pv(){
	$sql= "SELECT user_time,user_name,user_ip,user_url FROM user_online WHERE user_time>'".T_TIME."' AND siteid='".WAPID."'";   
	$online = mysql_query($sql);
	return mysql_num_rows($online);
}
function month_ip(){
	$sql= 'SELECT visitsip_y,visitsip_m,visitsip_d,visitsip_all,visitspv_y,visitspv_m,visitspv_d,visitspv_all,visits_date FROM visits WHERE visits_date>\''.M_TIME.'\';';   
		$online = mysql_query($sql);
		if($online && mysql_num_rows($online)>0){
			while(list($visitsip_y,$visitsip_m,$visitsip_d,$visitsip_all,$visitspv_y,$visitspv_m,$visitspv_d,$visitspv_all,$visits_date) = mysql_fetch_row($online))
			{
				$M_IP += $visitsip_d;
			}
		}
	return $M_IP; 
}
function month_pv(){
	$sql= 'SELECT visitsip_y,visitsip_m,visitsip_d,visitsip_all,visitspv_y,visitspv_m,visitspv_d,visitspv_all,visits_date FROM visits WHERE visits_date>\''.M_TIME.'\';';   
		$online = mysql_query($sql);
		if($online && mysql_num_rows($online)>0){
			while(list($visitsip_y,$visitsip_m,$visitsip_d,$visitsip_all,$visitspv_y,$visitspv_m,$visitspv_d,$visitspv_all,$visits_date) = mysql_fetch_row($online))
			{
				$M_PV += $visitspv_d;
			}
		}
	return $M_PV+1; 
}
function year_ip(){
	$sql= 'SELECT visitsip_y,visitsip_m,visitsip_d,visitsip_all,visitspv_y,visitspv_m,visitspv_d,visitspv_all,visits_date FROM visits WHERE visits_date>\''.Y_TIME.'\';';   
		$online = mysql_query($sql);
		if($online && mysql_num_rows($online)>0){
			while(list($visitsip_y,$visitsip_m,$visitsip_d,$visitsip_all,$visitspv_y,$visitspv_m,$visitspv_d,$visitspv_all,$visits_date) = mysql_fetch_row($online))
			{
				$Y_IP += $visitsip_d;
			}
		}
	return $Y_IP; 
}
function year_pv(){
	$sql= 'SELECT visitsip_y,visitsip_m,visitsip_d,visitsip_all,visitspv_y,visitspv_m,visitspv_d,visitspv_all,visits_date FROM visits WHERE visits_date>\''.Y_TIME.'\';';   
		$online = mysql_query($sql);
		if($online && mysql_num_rows($online)>0){
			while(list($visitsip_y,$visitsip_m,$visitsip_d,$visitsip_all,$visitspv_y,$visitspv_m,$visitspv_d,$visitspv_all,$visits_date) = mysql_fetch_row($online))
			{
				$Y_PV += $visitspv_d;
			}
		}
	return $Y_PV+1; 
}
function ip_all(){
	$sql= 'SELECT visitsip_y,visitsip_m,visitsip_d,visitsip_all,visitspv_y,visitspv_m,visitspv_d,visitspv_all,visits_date FROM visits;';   
		$online = mysql_query($sql);
		if($online && mysql_num_rows($online)>0){
			while(list($visitsip_y,$visitsip_m,$visitsip_d,$visitsip_all,$visitspv_y,$visitspv_m,$visitspv_d,$visitspv_all,$visits_date) = mysql_fetch_row($online))
			{
				$IP_ALL += $visitsip_d;
			}
		}
	return $IP_ALL; 
}
function pv_all(){
	$sql= 'SELECT visitsip_y,visitsip_m,visitsip_d,visitsip_all,visitspv_y,visitspv_m,visitspv_d,visitspv_all,visits_date FROM visits ;';   
		$online = mysql_query($sql);
		if($online && mysql_num_rows($online)>0){
			while(list($visitsip_y,$visitsip_m,$visitsip_d,$visitsip_all,$visitspv_y,$visitspv_m,$visitspv_d,$visitspv_all,$visits_date) = mysql_fetch_row($online))
			{
				$PV_ALL += $visitspv_d;
			}
		}
	return $PV_ALL+1; 
}
function put_logs(){
	
	$sql= 'SELECT visitsip_y,visitsip_m,visitsip_d,visitsip_all,visitspv_y,visitspv_m,visitspv_d,visitspv_all,visits_date FROM visits WHERE visits_date=\''.T_TIME.'\';';   
	$online = mysql_query($sql);
	
	if($online && mysql_num_rows($online)>0){
		
	list($visitsip_y,$visitsip_m,$visitsip_d,$visitsip_all,$visitspv_y,$visitspv_m,$visitspv_d,$visitspv_all,$visits_date) = mysql_fetch_row($online);
	
	$today_ip = today_ip();
	$today_pv = today_pv();
	$month_ip = month_ip();
	$month_pv = month_pv();
	$year_ip  = year_ip();
	$year_pv  = year_pv();
	$ip_all   = ip_all();
	$pv_all   = pv_all();
	
	$sql = 'UPDATE `visits` SET `visitsip_d` = \''.$today_ip.'\',`visitspv_d` = \''.$today_pv.'\',`visitsip_m` = \''.$month_ip.'\',`visitspv_m` = \''.$month_pv.'\',`visitsip_y` = \''.$year_ip.'\',`visitspv_y` = \''.$year_pv.'\',`visitsip_all` = \''.$ip_all.'\',`visitspv_all` = \''.$pv_all.'\' WHERE `visits_date` = \''.T_TIME.'\';';
	
	mysql_query($sql);
	
	}else{
		$today_ip = today_ip();
		$today_pv = today_pv();
		$sql = 'INSERT INTO visits (visitsip_y,visitsip_m,visitsip_d,visitsip_all,visitspv_y,visitspv_m,visitspv_d,visitspv_all,visits_date) VALUES (\''.year_ip().'\',\''.month_ip().'\',\''.$today_ip.'\',\''.ip_all().'\',\''.year_pv().'\', \''.month_pv().'\', \''.$today_pv.'\', \''.pv_all().'\', \''.T_TIME.'\');';
		mysql_query($sql);
	}
}
////////////////////
//检测用户是否在线//
////////////////////
$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;
}