简单网站统计功能的实现 PV IP 真实访客数(UV)

主要统计三个数值,网站的pv 和ip 以及UV 真实访客数 因为有详细的统计记录,所以可以实现功能还有很大的扩展!把tallydate使用内存表的话,速度就会更快了


需要用到二张表

CREATE TABLE `tally` ( `date` date NOT NULL, `pvtotal` int(10) NOT NULL, `iptotal` int(10) NOT NULL, `dltotal` int(10) NOT NULL, PRIMARY KEY (`date`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `tallydata` ( `tdid` int(10) NOT NULL auto_increment, `ip` int(10) NOT NULL, `cookie` varchar(32) NOT NULL, `date` date NOT NULL, `time` int(10) NOT NULL, `uri` varchar(255) default NULL, `referer` varchar(255) default NULL, PRIMARY KEY (`tdid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

统计的调用文件(用于所在统计的页面进行js调入):

(注:因项目使用的是ThinkPHP框架,所以贴出的只是代码片段!因为代码刚完成,所以没有做优化以及注释)

<?php class TallyAction extends Action{ public function index(){ if($_SERVER['SERVER_NAME']!=‘fullyee.com’ && $_SERVER['SERVER_NAME']!=‘’ && $_SERVER['SERVER_NAME']!=‘company.fullyee.com’) exit; $add['ip']=ip2long(get_client_ip()); if(!Cookie::is_set(‘fullyeetally’)){ $value=md5(microtime().$add['ip'].rand()); $overTime=mktime(0,0,0,date(‘m’),date(‘d’)+1,date(‘Y’))-time(); Cookie::set("fullyeetally",$value,time()+$overTime); } $fullyeetally=Cookie::get(‘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); $now['date']=$nowDate; $now['iptotal']=$this->gototal($nowDate,‘ip’); $now['pvtotal']=$tallydata_->count(array(‘date’=>$nowDate),‘tdid’); $now['dltotal']=$this->gototal($nowDate,‘cookie’); if($tally=$tally_->find(array(‘date’=>$nowDate))){ $tally_->save(array(‘iptotal’=>$now['iptotal'],‘pvtotal’=>$now['pvtotal'],‘dltotal’=>$now['dltotal']),array(‘date’=>$nowDate)); }else{ $tally_->create($now); $tally_->add(); } $timeDel=time()-3600*24*50; $tallydata_->query("delete from `tallydate` where `time`<$timeDel"); echo ‘Success ‘+date(‘Y-m-d H:i:s’); } function gototal($nowDate,$a) { $tallydata_=D(‘Tallydata’); $now['iptotal']=$tallydata_->query("select count(distinct $a) from `tallydata` where `date`=’$nowDate’ "); return $now['iptotal'][0]["count(distinct $a)"]; } } ?>

程序中的 saveDate 部分,用于服务器的定时运行,最简单的方法就是用linux的crontab在一个访问人数比较少的时间wget一下saveDate代码就行了。。这段代码的作用是,把前一天的访问数据全部统计,以天为单位写入tally表!并且删除一定时间外的统计记录。默认是删除50天以前的全部统计记录

显示部分:

<?php class configAction extends Action{ function _initialize(){ header("Content-Type:text/html; charset=utf-8"); } public function tally() { $tally_=D(‘Tally’); $tallydata_=D(‘Tallydata’); $nowDate=date(‘Y-m-d’); $now['date']=$nowDate; $now['iptotal']=$this->gototal($nowDate,‘ip’); $now['pvtotal']=$tallydata_->count(array(‘date’=>$nowDate),‘tdid’); $now['dltotal']=$this->gototal($nowDate,‘cookie’); if($tally=$tally_->find(array(‘date’=>$nowDate))){ $tally_->save(array(‘iptotal’=>$now['iptotal'],‘pvtotal’=>$now['pvtotal'],‘dltotal’=>$now['dltotal']),array(‘date’=>$nowDate)); }else{ $tally_->create($now); $tally_->add(); } $today['pv']=$now['pvtotal']; $today['ip']=$now['iptotal']; $today['dl']=$now['dltotal']; $yesterdayDate=date(‘Y-m-d’,time()-3600*24); //echo $yesterdayDate; $yesterday=$tally_->find(array(‘date’=>$yesterdayDate)); //dump($yesterday); $yesterday['pv']=isset($yesterday['pvtotal'])?$yesterday['pvtotal']:’0′; $yesterday['ip']=isset($yesterday['iptotal'])?$yesterday['iptotal']:’0′; $yesterday['dl']=isset($yesterday['dltotal'])?$yesterday['dltotal']:’0′; $maxpv=$this->gomax(‘pvtotal’); $maxip=$this->gomax(‘iptotal’); $maxdl=$this->gomax(‘dltotal’); $max['pv']=$maxpv['pvtotal']; $max['pvdate']=$maxpv['date']; $max['ip']=$maxip['iptotal']; $max['ipdate']=$maxip['date']; $max['dl']=$maxdl['dltotal']; $max['dldate']=$maxdl['date']; $this->assign(‘today’,$today); $this->assign(‘yesterday’,$yesterday); $this->assign(‘max’,$max); $this->assign(‘nowtime’,date(‘Y年m月d日 H:i:s’)); $this->display(); } function gomax($a) { $tally_=D(‘Tally’); $max=$tally_->query("select * from `tally` order by `$a` desc limit 1"); return $max[0]; } function gototal($nowDate,$a) { $tallydata_=D(‘Tallydata’); $now['iptotal']=$tallydata_->query("select count(distinct $a) from `tallydata` where `date`=’$nowDate’ "); return $now['iptotal'][0]["count(distinct $a)"]; } } ?>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11979.html