您好,欢迎访问一九零五行业门户网

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= $all+ count($ta);
        }
        return $all;
    }
function getcountuseimg($day=1)    
    {
        $allnumber = $this->getcountbyday($day);
        for($i=0;$istrlen($allnumber);$i++)
        {
            $idx = substr($allnumber,$i,1);
            $returnval.= sprintf(,$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);
?>
其它类似信息

推荐信息