搜尋
首頁php教程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
);
?>

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具