Home > php教程 > php手册 > 基于IP的简单网站访问量统计(已更新版优化SQL数据库)

基于IP的简单网站访问量统计(已更新版优化SQL数据库)

WBOY
Release: 2016-06-07 11:36:46
Original
1013 people have browsed it

基于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元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template