<?php/** * * 计数器 * * Version : 1.0.0 * Author : kemy * PHP : >4.3 */class Counter{ var $CountFolder; function Counter() { $this->MakeCounter(); } function MakeCounter() { $this->CountFolder = "counter"; if(!is_dir($this->CountFolder)) { mkdir($this->CountFolder,0777); } $this->_DoCount(); } //Save data file function _DoCount() { $file ="counter.txt"; $ip2long=ip2long($_SERVER['REMOTE_ADDR']); if(strstr($ip2long,"-")){ } else{ $ip2long="-".$ip2long; } if ($ip2long === -1) { //echo "Invalid IP, please try again"; }else{ $pathfile=$this->CountFolder."/".$file; $content=date("Ymd").$ip2long; $oldfilecontent= file_get_contents($pathfile); $pos=strpos($oldfilecontent,$content); if($pos===false){ $fp = @fopen($pathfile,"a+"); @fputs($fp,$content." "); @fclose($fp); } } } //获得指定时间的统计数据 //默认为1天 0 为取所有 function GetCountByDay($day=1,$isall=true) { //今日的时间戳 $all = 0; $folder = "counter/counter.txt"; $Y=date("Y"); $m=date("m"); $d=date("d"); if(0==$day){ $ta=file($folder); $all=count($ta); } else{ if($isall){ for($i=$day-1;$i>=0;$i--) { $str= file_get_contents($folder); $dd=$d-$i; $date=date ("Ymd", mktime (0,0,0,$m,$dd,$Y)); preg_match_all("/$date-(d+)s+/",$str,$matches); $ta = $matches[0]; $all= $all+ count($ta); } } else{ $str= file_get_contents($folder); $dd=$d-$day+1; $date=date ("Ymd", mktime (0,0,0,$m,$dd,$Y)); preg_match_all("/$date-(d+)s+/",$str,$matches); $ta = $matches[0]; $all= $all+ count($ta); } } return $all; } function GetCountUseImg($day=1,$imgArray,$isall=true) { $allnumber = $this->GetCountByDay($day,$isall); for($i=0;$i<strlen($allnumber);$i++) { $idx = substr($allnumber,$i,1); $returnVal.= sprintf("<img src="/img/%s">",$imgArray[$idx]); } return $returnVal; }}$imgArray = array("0.png","1.png","2.png","3.png","4.png","5.png","6.png","7.png","8.png","9.png",);$counter = new Counter();/*echo "<hr size=1>今日访问量为:".$counter->GetCountUseImg(1,$imgArray,false);echo "<hr size=1 /> 昨日访问量为:".$counter->GetCountByDay(2,false);echo "<hr size=1 />今昨日访问量为:".$counter->GetCountByDay(2);echo "<hr size=1 />访问量为:".$counter->GetCountUseImg(0,$imgArray);*///echo "总浏览量:".$counter->GetCountUseImg(0,$imgArray)."今日访问数:".$counter->GetCountUseImg(1,$imgArray,false)."昨日访问数:".$counter->GetCountUseImg(2,$imgArray,false)."开始统计日:2007-11-18"echo "总浏览量:<font color=blue >".$counter->GetCountByDay(0)."</font> 今日访问数:<font color=blue >".$counter->GetCountByDay(1,false)."</font> 昨日访问数:<font color=blue>".$counter->GetCountByDay(2,false)."</font> 开始统计日:xxxx-xx-xx"?>