<?
error_reporting(0);
$type=$_REQUEST[ctype];
$logdir="./logs/";
$sufix=".txt";
$ip_log="./logs/ip.txt";
$timeout= 24; 
$day=7;
$total="./logs/total.txt";
function checkIp($logip){
  global $ip_log,$timeout;
  $now = time();
  $find = FALSE;	
  $ip_arr=file($ip_log);
  $check=FALSE;
  $fp = fopen($ip_log, "w");
  while(list(,$ips)=each($ip_arr)){
  	list($ip,$time)=explode("|",$ips);
  	if(($now-$time)<$timeout*3600){
  		if($logip==$ip){
  			$check=TRUE;	
  		}else{
  			fputs($fp,"$ip|$time\n");	
  	}
  	}
}
	  fputs($fp, "$logip|$now|\n");
	  fclose($fp);
  	  return $check;
}

//log
$now_date = date("Y/m/d[D]\tH:i:s", time());
$prefix = date("Ymd", time()); 
$filename=$prefix.$sufix;
$ip=getenv("REMOTE_ADDR");
$agent = getenv("HTTP_USER_AGENT");
$agent = str_replace("\t", "", $agent);
$ref = getenv("HTTP_REFERER");
if($ref=="") $ref = "none";
if(strrpos($ref,"?")==strlen($ref)-1) $ref=substr($ref, 0, -1);
$ref = str_replace("\t", "", $ref);
if(!checkIP($ip)) $uniq = 1;
$logfile = $logdir . $filename;
$data = "$now_date\t$ip\t$agent\t$ref\t$uniq\n";
$fp = fopen($logfile,"a");
fputs($fp, $data);
fclose($fp);
if(file_exists($total)){
	$fp=fopen($total,"a");
	fputs($fp,"1");
	fclose($fp);
}else{
	$fp=fopen($total,"w");
	fputs($fp,"1");
	fclose($fp);
}
$tt= $logdir . "total_".$filename;
if(file_exists($tt)){
	$fp=fopen($tt,"a");
	fputs($fp,"1");
	fclose($fp);
}else{
	$fp=fopen($tt,"w");
	fputs($fp,"1");
	fclose($fp);
}
$hits = filesize($total);
$thits = filesize($tt);

 if($type==img){
	$count=$total;
	$len=strlen($count);			//总访量位数
	if($len < 7){$rest=7-$len;}		//至少显示7位数字
	else{$rest=2;}
	for ($i=1;$i<=$rest;$i++) {
		$count="0".$count;		//补足0
	}

	for ($i=0;$i<strlen($count);$i++) {
		$number=substr($count,$i,1);
		$images="<img src='images/number/$number.gif' border=0>";

		$show="$show"."$images";
	}
	echo "document.write(\$show\")";
 }
 elseif($type=="hidden"){}
 else{echo "document.write(\"Today $thits Total  $hits \")";}

?>