php数据库查询总的访问量,PHP — 统计总访问量,无需涉及数据库的纯文件操作...

方法一:记录访问总数和每次访问的日志

//====================

//文件: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   ” %E2%80%98%24basedir%24ok%5B%24i%5D “; 以图片做显示就输出此行,将下面的echo $counter_info;隐藏

}

echo $counter_info;

}

counter();

?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值