TP框架简单网站统计功能的实现 PV IP UV

public function save(){
        $add['ip']=ip2long(get_client_ip());
        if(!cookie('fullyeetally')){
            $value=md5(microtime().$add['ip'].rand());

            $overTime=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-time();
            cookie("fullyeetally",$value,time()+$overTime);
        }
        $fullyeetally=cookie('fullyeetally');
        $add['cookie']=$fullyeetally;
        $add['date']=date('Y-m-d');
        $add['time']=time();
        $add['uri']=$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $add['referer']=$_SERVER['HTTP_REFERER'];
        $tallydata_=D('Tallydata');
        $tallydata_->create($add);
        $tallydata_->add();

    }

    public function saveData()
    {
        $tally_=D('Tally');
        $tallydata_=D('Tallydata');
        $nowDate=date('Y-m-d',time()-3600*24);
        $condition['date'] = $nowDate;
        $condition['school'] = $school;
        $now['date']=$nowDate;
        $now['iptotal']=$this->gototal($nowDate,'ip');
        $now['pvtotal']=$tallydata_->where($condition)->count('tdid');
        $now['dltotal']=$this->gototal($nowDate,'cookie');
        $now['school']=$school;

        if($tally=$tally_->where(array('date'=>$nowDate))->find()){
            return;
            /*$tally_->where($condition)
                ->save(array(
                    'iptotal'=>$now['iptotal'],
                    'pvtotal'=>$now['pvtotal'],
                    'dltotal'=>$now['dltotal']
                ));*/
        }else{
            $tally_->create($now);
            $tally_->add();
        }
        $timeDel=time()-3600*24*50;
//        $tallydata_->query("delete from `tallydate` where `time`<$timeDel");
        $tallydata_->where('time<"'.$timeDel.'"')->delete();
//        echo 'Success'.date('Y-m-d H:i:s');
    }

    function gototal($nowDate,$a)
    {
        $tallydata_=D('Tallydata');
        $condition2['date'] = $nowDate;
        $now['iptotal']=$tallydata_->distinct(true)
            ->field($a)
            ->where($condition2)
            ->select();
//        var_dump(count($now['iptotal']));
        return count($now['iptotal']);
    }

你可能感兴趣的:(常用)