Home >Backend Development >PHP Problem >How to achieve the number of visitors in php
How to achieve the number of visitors in php: 1. Create two database tables; 2. Add the code "$realip=getip();modifyipcount($realip);" on the page where you want to count the number of visitors. .
#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.
php How to achieve the number of visitors?
PHP accurately implements page visit statistics
1. Two database tables are required
①, IP record table
create table ip (ipid int(11) NOT NULL default '',ipdata varchar(16) NOT NULL default '',iptime varchar(30) NOT NULL default '', primary key(ipid));
Note: ipdata is the recorded ip of the visitor, iptime is the recorded ip access
②, statistics table
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, implementation method
In your case Put the following code on the page that counts the number of times:
$realip=getip(); modifyipcount($realip);
The code of the getip() function is:
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; }
Note: This function code can be found everywhere on the Internet
modifyipcount() function code is:
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); } } }
Note: Here I set the number of access statistics within 24 hours to only add 1
so that we can call todayipcount and allipcount in the database table count I got today's access IP and total access IP. I personally think it is very accurate. Everyone is welcome to put forward different opinions.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to achieve the number of visitors in php. For more information, please follow other related articles on the PHP Chinese website!