Просмотр файла Counter/maincode.php

Размер файла: 2.86Kb
<?php
$fonline="data/online.dat";
$fcont="data/count.dat";
$ftoday="data/today.dat";
$fhost="data/host.dat";
//Кто На Сайте?
$time=time();
$end_online_time=time()-300;
if ($_SERVER['HTTP_X_FORWARDED_FOR']) $user_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else $user_ip = $_SERVER['REMOTE_ADDR'];
if(!file_exists($fonline)){
	$fp=fopen($fonline,"w");
	fclose($fp);
}
$readonline=fopen($fonline,"r");
$str=@fread($readonline,filesize($fonline));
fclose($readonline);
if(preg_match ('#'.$user_ip.'#si', $str)){	
    $data="";
} else{
	$data=$user_ip."::".$time."\r\n";
}
$data_array=file($fonline);
$d=count($data_array);
for($i=0;$i<$d;$i++){	
	list($data_ip,$data_time)=explode("::","$data_array[$i]");
	if($data_ip!=""&&$data_time!=""){
		if($data_time<$end_online_time){			
			$data_ip="";
			$data_time="";
		}
		if($data_ip!=""&&$data_time!=""){			
			if($user_ip==$data_ip){
				$online_array[]="$user_ip::$time\r\n";
			}
			else
			$online_array[]="$data_ip::$data_time";
		}
	}
}
if(isset($online_array)){
	foreach($online_array as $i=>$str){
		if($str=="$user_ip::$time\r\n"){
			$ok=$i;
        	break;
	    }
	}
	foreach($online_array as $j=>$str){		
	    if($ok==$j) { $online_array[$ok]="$user_ip::$time\r\n"; break;}
	}
}
$user_ip=htmlspecialchars(stripslashes($user_ip));
$wd=@fopen($fonline,"w");
@flock($wd,2);
if($online_array=="") $online_array[]="$user_ip::$time\r\n";
foreach($online_array as $str)
fputs($wd,"$str");
@flock($wd,3);
@fclose($wd);
$time_format=getdate($time);
if(isset($_COOKIE['PROtS_Count'])){
	$cookie_count = explode("::", $_COOKIE['PROtS_Count']);
	$cookie_count['2']++;
	$cookie_exp = time() + 3600*4;	
	setcookie("PROtS_Count", $cookie_count['0']."::".$cookie_count['1']."::".$cookie_count['2'], $cookie_exp, "", "", "0");
}else{
	$count_usr=$user_ip."::Online::2";
	$cookie_exp = time() + 3600*24;
	setcookie("PROtS_Count", $count_usr, $cookie_exp, "", "", "0");	
	if(!file_exists($fcont)){	
		$fp=fopen($fcont,"w");
        fwrite($fp,0);
        fclose($fp);
    }if(!file_exists($ftoday)){
		$fp=fopen($ftoday,"w");
        fwrite($fp,0);
        fclose($fp);
	}
	//Общий Счетчик
	$fp=fopen($fcont,"r");
    $count=@fread($fp,filesize($fcont));
    fclose($fp);    
	$fp=fopen($fcont,"w");
	$count++;
    fwrite($fp,$count); 
    fclose($fp);
    //Ежидневный Счетчик
	$fp=fopen($ftoday,"r");
    $today=@fread($fp,filesize($ftoday));
    fclose($fp);    
	$fp=fopen($ftoday,"w");
	$today++;
    fwrite($fp,$today); 
    fclose($fp);
	//Хосты
    $host_ip=$user_ip."\r\n";
    $fp=fopen($fhost,"a+");
    $ip_buffer=@fread($fp,filesize($fhost)); 
    fclose($fp);
    if (preg_match ("/\b".$user_ip."\b/i", $ip_buffer)){}
    else{    	    	
		$fp=fopen($fhost,"a");
        fwrite($fp,$host_ip); 
        fclose($fp);	  
	}   
}
?>