+ -
当前位置:首页 → 问答吧 → 发布一个前一些日子写的统计访问类

发布一个前一些日子写的统计访问类

时间:2008-08-06

来源:互联网

复制内容到剪贴板
代码:
<?php
/***********************************************************
* Document Type: Classes
* Update: 2008/07/09
* Author: Jimmy
* Remark: Access statistics
* mail:[email protected]
* personal homepage: www.yangzi520.com
***********************************************************/

class visitCount {
        private $today_v = 0;                         //today visit count
        private $total_v = 0;                         //total visit count
        private $cookname = "ziyuCountV";
        private $filename;                            //file path and name
        private $dateV;
        private $timezone = "Asia/Shanghai";          //timezone set
       
       
        function __construct($today_v="",$total_v=""){   //construct function
            $this->filename = __SITE_ROOT ."/visitCount.txt";
                ini_set('date.timezone',$this->timezone);
                $fp = fopen($this->filename,"a+") or die("conn't open $this->filename");
                fclose($fp);
                if(!empty($today_v)&&!empty($total_v)){       //initialize the class   
                        $this->today_v = $today_v;
                        $this->total_v = $total_v;
                        $this->dateV = date("Ymd");
                       
                }else{
                        if(!$visitRe=file($this->filename)){
                                echo  "read file content failed1";
                                return false;       
                        }
                        $this->today_v = trim($visitRe[0]);
                        $this->total_v = trim($visitRe[1]);
                        $this->dateV = trim($visitRe[2]);
                }
                if($this->dateV != date("Ymd")){
                        $this->today_v = 0;
                }
               
        }
       
        function cookie_exist(){
                return $vCookie_exist=!empty($_COOKIE[$this->cookname])?true:false;
        }
       
        function cookie_write(){
                $reslut_w=setcookie($this->cookname,time(),time()+24*3600);
                return $reslut_w;
               
        }
       
        function compare_today(){
                $date_cook = date("Ymd",$_COOKIE[$this->cookname]);
                $date_curr = date("Ymd");
                if($date_cook == $date_curr){
                        return true;
                }else{
                        return false;
                }
        }
       
        function count_ope(){
                if(!$this->cookie_exist()){
                        if(!$this->cookie_write()){  //set cookie
                                return false;
                        }       
                        $this->today_v+=1;
                        $this->total_v+=1;
                }else{
                        if(!$this->compare_today()){
                                $this->today_v++;
                                $this->total_v++;
                        }
                        if(!$this->cookie_write()){ //update cookie
                                return false;
                        }
                       
                       
                }
               
               
        }
        function getVisitRe($type){
                if(!$visitRe=file($this->filename)){
                        echo  "read file content failed2";
                        return false;       
                        }
                switch ($type){
                        case "today_v":
                                $visitRe_txt = trim($visitRe[0]);
                                 break;
                        case "total_v":
                                $visitRe_txt = trim($visitRe[1]);
                                break;
                        case "date":
                                $visitRe_txt = trim($visitRe[2]);
                                break;
                        case "1":
                                $visitRe_txt = trim($visitRe[0]);
                                 break;
                        case "2":
                                $visitRe_txt = trim($visitRe[1]);
                                break;
                        case "3":
                                $visitRe_txt = trim($visitRe[2]);
                                break;
                        default:
                                echo "you input the wrong parameter";
                                return false;
                }       
                return $visitRe_txt;
               
        }
                       
       
       
        function updatetxt(){
                if(!$fh=fopen($this->filename,"w+")){
                        echo "failed open the $this->filename";       
                }
                $updateArr = array($this->today_v,$this->total_v,date("Ymd"));
                foreach ($updateArr as $k => $value){
                        //echo $k;
                        if (fwrite($fh, $value."\r\n") === FALSE) {
                        echo "cann't write $this->filename";
                        exit;
                    }
                }
               
            fclose($fh);
        }
       
       
        function count_visit(){
                if(!$this->cookie_exist()){
                        if(!$this->cookie_write()){  //set cookie
                                return false;
                        }       
                        $this->today_v+=1;
                        $this->total_v+=1;
                        $this->updatetxt();
                }else{
                        if(!$this->compare_today()){
                                $this->today_v++;
                                $this->total_v++;
                                $this->updatetxt();
                        }
                        if(!$this->cookie_write()){ //update cookie
                                return false;
                        }
                       
                       
                }
       
       
        }
}



?>
这是一个以自类天数为分隔的简单统计访问量的类,记录当天访问量和历史访问量的类,以文本记录访问量,方便移植。跟网站其它程序关联性很小。
当然了,并不很完美,你完全可以根椐自已的需要,稍改下用于自已的程序。
欢迎大家讨论,欢迎大家的鲜花和掌声,同时不介意你们的砖头。
对了,少说一点,这个类还可以设置时区哦。

作者: piaomiao227   发布时间: 2008-08-06

沙发啊

作者: designline   发布时间: 2008-08-08

代码高亮下吧
复制PHP内容到剪贴板
PHP代码:
代码

作者: lxylxy888666   发布时间: 2008-08-08

如何高亮?

作者: piaomiao227   发布时间: 2008-08-11