基于ip的简单网站访问量统计,同一ip下一天只算一次的访问量 代码如下
mysql 代码
create table ipcount(
ip int(10) primary key not null
auto_increment,
nowdate varchar(255),
nowdatec varchar(255),
ip varchar(255)
)
b]控制器代码[[/b]
public function ipcount(){
$ip=get_client_ip(); //获取客户端ip
ini_set('date.timezone','asia/shanghai');//设置时区
$date=date('y-m-d');//获取当前时间
$yesterday=date(y-m-d,strtotime(-1 day)); //获取昨天时间
$yesterrow=m('ipcount')->field('nowdatec')->where('nowdate='.$yesterday.'')->select();
$yesterdayc=0;
for ($i=0;$i $yesterdayc+=$yesterrow[$i]['nowdatec'];
}
$row=m('ipcount')->field('ip')->where('nowdate='.$date.'')->select();//查找今天的记录
$n=1;
$add=array(
'nowdatec'=>$n,
'nowdate'=>$date,
'ip'=>$ip,
);
if(empty($row)){//判断并添加记录
m('ipcount')->add($add);
}
$iprow=m('ipcount')->field('ip')->where('ip like %'.$ip.'% and nowdate='.$date.'')->select();//查找今天的ip记录
$ipcount=$row[0]['ip'];
if(empty($iprow)){ //判断并更新ip和统计记录
$ipcount=$ipcount.$ip;
$row1=m('ipcount')->field('nowdatec')->where('nowdate='.$date.'')->select();
foreach($row1 as $cd){
$dd= $cd['nowdatec'];
}
$dd+=1;
$save=array(
'nowdatec'=>$dd,
'nowdate'=>$date,
'ip'=>$ipcount,
);
m('ipcount')->where('nowdate='.$date.'')->save($save); //判断并更新ip和统计记录
}
$nowrow=m('ipcount')->field('nowdatec')->where('nowdate='.$date.'')->select();
$nowsun=0;
for ($i=0;$i $nowsun+=$nowrow[$i]['nowdatec'];
}
if(!empty($nowsun)){
echo '今天访问量:',$nowsun,'';//判断输出记录
}else{
echo '今天访问量:0';
};
$rows=m('ipcount')->field('nowdatec')->select();
$sun=0;
for ($i=0;$i $sun+=$rows[$i]['nowdatec'];
}
if(!empty($yesterdayc)){
echo '昨天访问量:',$yesterdayc,'';//判断输出记录
}else{
echo '昨天访问量:0';
}
if(!empty($sun)){
echo '总访问量:',$sun,'';//判断输出记录
}else{
echo '总访问量:0';
}
}
ad:真正免费,域名+虚机+企业邮箱=0元