PHP准确的实现页面访问统计次数uv
1、需要两个数据库表
①、IP记录表
create table ip (ipid int(11) NOT NULL default '',ipdata varchar(16) NOT NULL default '',iptime varchar(30) NOT NULL default '', primary key(ipid));
注:ipdata为记录的访问者的ip,iptime为记录的该ip访问
②、统计次数统计表
create table count (todayipcount int(11) NOT NULL default '',allipcount int(11) NOT NULL default '',day varchar(2) NOT NULL default '');
insert into count (todayipcount,allipcount,day) values ('0','0','0');
2. 实现方法
在你要统计的页面放入代码
$realip=getip(); modifyipcount($realip);
getip()函数的代码为:
function getip() { if (isset($_SERVER)) { if (isset($_SERVER[HTTP_X_FORWARDED_FOR]) && strcasecmp($_SERVER[HTTP_X_FORWARDED_FOR], "unknown"))//代理 { $realip = $_SERVER[HTTP_X_FORWARDED_FOR]; } elseif(isset($_SERVER[HTTP_CLIENT_IP]) && strcasecmp($_SERVER[HTTP_CLIENT_IP], "unknown")) { $realip = $_SERVER[HTTP_CLIENT_IP]; } elseif(isset($_SERVER[REMOTE_ADDR]) && strcasecmp($_SERVER[REMOTE_ADDR], "unknown")) { $realip = $_SERVER[REMOTE_ADDR]; } else { $realip = 'unknown'; } } else { if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) { $realip = getenv("HTTP_X_FORWARDED_FOR"); } elseif(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) { $realip = getenv("HTTP_CLIENT_IP"); } elseif(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) { $realip = getenv("REMOTE_ADDR"); } else { $realip = 'unknown'; } } return $realip; } 注:此函数代码网上到处都有
modifyipcount()函数的代码为:
function modifyipcount($ip) { <-----------------------数据库的连接省略-------------------------> $query="SELECT * FROM ip where ipdata='".$ip."'"; $result=mysql_query($query); $row=mysql_fetch_array($result); $iptime=time(); $day=date('j'); if(!$row) { $query="INSERT INTO ip (ipdata,iptime) VALUES ('".$ip."','".$iptime."')"; mysql_query($query); $query="SELECT day,todayipcount,allipcount FROM count"; $result=mysql_query($query); $row=mysql_fetch_array($result); $allipcount=$row['allipcount']+1; $todayipcount=$row['todayipcount']+1; if($day==$row['day']) { $query="UPDATE count SET allipcount='".$allipcount."',todayipcount='".$todayipcount."'"; } else { $query="UPDATE count SET allipcount='".$allipcount."',day='".$day."',todayipcount='1'"; } mysql_query($query); } else { $query="SELECT iptime FROM ip WHERE ipdata='".$ip."'"; $result=mysql_query($query); $row=mysql_fetch_array($result); $query="SELECT day,todayipcount,allipcount FROM count"; $result=mysql_query($query); $row1=mysql_fetch_array($result); if($iptime-$row['iptime']>86400) { $query="UPDATE ip SET iptime='".$iptime."' WHERE ipdata='".$ip."'"; mysql_query($query); $allipcount=$row1['allipcount']+1; if($day==$row1['day']) { $query="UPDATE count SET allipcount='".$allipcount."'"; } else { $query="UPDATE count SET allipcount='".$allipcount."',day='".$day."',todayipcount='1'"; } mysql_query($query); } if($day!=$row1['day']) { $query="UPDATE count SET day='".$day."',todayipcount='1'"; mysql_query($query); } } } 注:这里我设置24小时内访问统计次数只加1
这样我们调用数据库表count中的todayipcount和allipcount就可以得到今日访问IP和总访问IP,个人觉得是很准确的,欢迎大家有不同意见提出。
原文链接:http://www.cnblogs.com/ovliverlin/articles/784918.html