方法一:记录访问总数和每次访问的日志
//====================
//文件:count.php
//功能:计数|记录
//====================
$filename = “count.txt”;
$logs= “logs.txt “;
$ip=$REMOTE_ADDR;
$Today=date( “Y年m月d日 H:i:s “);
$handle = fopen ($filename, “a+”) or exit(“Unable to open file!”);
$contents = “”;
if(!feof($handle)) {
$contents = fread($handle,filesize($filename));
}
fclose($handle);
$handle=fopen($filename, “w”);
if($contents== ” “)$contents=0;
if(is_numeric($contents))$contents+=1;
fputs($handle,$contents);
echo $contents;
fclose($handle);
//$handle=fopen($logs, “a “);
//$log_text=$contents. ” > “.$ip. ” 在 “.$Today. “访问\n “;
//fputs($handle,$log_text);
//fclose($handle);
//————————————————————————
方法二:支持内部/外部ip统计,日访问量统计
$yourip=$REMOTE_ADDR. “ “.$HTTP_USER_AGENT;
$yourip_min=$REMOTE_ADDR;
function IPisOutside($ip){
if(substr($ip,0,3)== “192 ” || substr($ip,0,3)== “172 ” || substr($ip,0,3)== “127 “){
return false;
}
return true;
}
function counter()
{
global $yourip;
global $yourip_min;
$basedir= “”; // should be terminated with “/ ”
$counterFile=$basedir. “counter_.txt “;
if(!file_exists($counterFile)){
//die( “计数器记录文件丢失! “);
$fp=fopen($counterFile, “w+”);
fputs($fp, ” “);
fclose($fp);
}
$fd=@fopen($counterFile, “r+”);
$str=@fread($fd,255);
$str_array=@explode( “|| “,$str);
while(list($key,$value)=each($str_array)){
$count[$key]=$value;
}
@fclose($fd);
$num=$count[0]*1;
$ip=$count[1];
$total_count_in_out_tmp=@explode( “, “,$count[2]);
$total_count_in=$total_count_in_out_tmp[0]*1;
$total_count_out=$total_count_in_out_tmp[1]*1;
$total_days=$count[7]*1;
$average_day_in=$count[4]*1;
$p1_array=@explode( “, “,$count[3]);
$in_date_count=$p1_array[0]*1;
$in_date=$p1_array[1];
$average_day_out=$count[6];
$p2_array=@explode( “, “,$count[5]);
$out_date_count=$p2_array[0]*1;
$out_date=$p2_array[1];
if (date( “Y-m-d “) <> “$in_date “){
$total_days++;
$average_day_in= round( ($in_date_count + $average_day_in*($total_days-1))/$total_days);
$in_date=date( “Y-m-d “);
$average_day_out= round( ($out_date_count + $average_day_out*($total_days-1))/$total_days);
$out_date=date( “Y-m-d “);
$in_date_count=0;
$out_date_count=0;
}
if($ip!=$yourip){ //若想不以IP统计,请注释此行
$num++;
if(IPisOutside($yourip)==false){
$total_count_in++;
$in_date_count++;
}else{
$total_count_out++;
$out_date_count++;
}
}
$fd=@fopen($counterFile, “w+”);
fputs($fd, “$num||$yourip||$total_count_in,$total_count_out||$in_date_count,$in_date||$average_day_in||$out_date_count,$out_date||$average_day_out||$total_days “);
fclose($fd);
$average_day_total=$average_day_in+$average_day_out;
$counter_info= ” \n总访问量 $num “. “\n内部 $total_count_in “. “\n外部 $total_count_out “. “\n—-\n平均日访问量 $average_day_total “. “\n内部 $average_day_in “. “\n外部 $average_day_out “. “\n—-\n你的IP是 $yourip_min “;
$count_len=strlen($num);
for($i=0;$i
{
$ok[$i]=substr($num,$i,1). “.gif “;
//echo ” “; 以图片做显示就输出此行,将下面的echo $counter_info;隐藏
}
echo $counter_info;
}
counter();
?>