search
Homephp教程PHP源码php实现计数器功能

<script>ec(2);</script>


/**
 *
 *  计数器(v1.1.0)
 *
 *  Version : 1.1.0
 *  Author  : 村长
 *  PHP     : >4.3
 *  
 */
class 
Counter
{
    var 
$CountFolder
;
    var 
$countType
;
    var 
$showtype
;
    var 
$imgArray
;

    function 
Counter
()
    {
        
$this->countType 0
;
        
$this->showtype "GetCountByDay"
;
        
$this->imgArray 
= array();
        
$this->MakeCounter
();
    }

    function 
MakeCounter
()
    {
        
$this->CountFolder "log/".date("Ymd"
);
        if(!
is_dir($this->CountFolder
))
        {
            
mkdir($this->CountFolder,0777
);
        }
        
$this->_DoCount
();
    }

    
//Save data file
    
function _DoCount
()
    {
        
$file date("Ymd").ip2long($_SERVER['REMOTE_ADDR']).".txt"
;
        if(!
file_exists($this->CountFolder."/".$file
))
        {
            
$fp = @fopen($this->CountFolder."/".$file,"wb+"
);
            
//这里可以保存其他需要的日志记录
            
@fputs($fp,$_SERVER['REMOTE_ADDR'
]);
            @
fclose($fp
);
        }
    }

    
//获得指定时间的统计数据
    //默认为1天 0 为取所有
    
function GetCountByDay($day=1
)
    {
        
//今日的时间戳
        
$today strtotime(date("Y-m-d"
));
        
$all 0
;
        for(
$i=$day;$i>=0;$i
--)
        {
            
$tsp $today-(86400*$i
);
            
$folder "log/".date("Ymd",$tsp)."/*.txt"
;
            
$ta glob("$folder"
);
            
$all$allcount($ta
);
        }
        return 
$all
;
    }

    function 
GetCountUseImg($day=1
)    
    {
        
$allnumber $this->GetCountByDay($day
);
        for(
$i=0;$istrlen($allnumber);$i
++)
        {
            
$idx substr($allnumber,$i,1
);
            
$returnVal.= sprintf("php实现计数器功能",$this->imgArray[$idx
]);
        }
        return 
$returnVal
;
    }    

    function 
SetStyle($style='',$ext="gif"
)
    {
        
$imgprefix ="img/"
;
        
$folder $imgprefix.$style
;
        foreach(
range(0,9) as $num
)
        {
            
$this->imgArray[$num] = $folder."/".$num.".$ext"
;
        }
        if(
is_dir($folder
))
        {
            
$this->showtype "GetCountUseImg"
;
        }
        else 
        {
            
$this->showtype "GetCountByDay"
;
        }
    }
    
    function 
Show($day=0
)
    {
        return 
$this->{$this->showtype}($day
);
    }
}

//实例计数器对象
$counter = new Counter
();

//采用 sports98 样式显示,告知默认扩展名为png格式
$counter->SetStyle("sports98","png"
);
echo 
"
今日访问:"
.$counter->Show(0
);

//采用S1样式显示,默认扩展名为 gif
$counter->SetStyle("s1"
);
echo 
"
近3日访问:"
.$counter->Show(2
);

//----只显示文字的计数器
$counter->SetStyle("-"
);
echo 
"
近3日访问:"
.$counter->Show(2
);
?>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),