php简单统计类
来源:互联网 发布:大型网络监控系统方案 编辑:程序博客网 时间:2024/04/28 11:18
<?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"
?>
/**
*
* 计数器
*
* 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"
?>
- php简单统计类
- php+dbfile开发简单的访客来源统计
- PHP处理HTML表单的一个简单例子 (活动统计)
- 简单统计
- php 简单分页类
- php简单分页类
- php简单上传类
- 简单php分页类
- PHP简单分页类
- php简单上传类
- php 简单分页类
- php 简单分页类
- php简单分页类
- 用php简单实现当页统计可以显示在当页
- php之统计器
- PHP统计目录数量
- 统计php源码行
- php统计在线人数
- 妖塔记
- DLL(Dynamic Link Libraries)专题[转帖]
- 完全犯罪
- 关于Silverlight安装问题之二
- Telelogic Tau UML 建模实例过程
- php简单统计类
- 数码锁
- 如何编写DLL文件
- 思维的惯性 数值设计的不可逆--风云
- 密室物语
- 测试自己对C++是否熟悉
- C#数据访问层
- SqlConnection,SqlCommand,SqlReader,SqlDataAdaper的用法总结!!
- 雪密室