How to realize the number of visitors by php
This article introduces the knowledge of "how to achieve the number of visitors in php". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Php to achieve the number of visitors: 1, create two database tables; 2, to count the number of pages to add the code "$realip=getip (); modifyipcount ($realip);" can be.
This article operating environment: Windows7 system, PHP7.4 version, Dell G3 computer.
How does php achieve the number of visitors?
PHP can accurately count the number of page visits
1. Two database tables are required
①, IP record sheet
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 ip,iptime of the recorded visitor and the ip access is recorded.
②, statistical table of statistical times
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
2. Implementation method
Put the following code on the page where you want to count the times:
$realip=getip (); modifyipcount ($realip)
The code for the getip () function is:
Function getip () {if (isset ($_ SERVER)) {if (isset ($_ server [http _ X_FORWARDED_FOR]) & & strcasecmp ($_ server [http _ X_FORWARDED_FOR], "unknown")) / / Agent {$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 ($_ realip [remote _ ADDR]) & & strcasecmp ($_ server [remote _ ADDR], "unknown")) {$realip = $_ server [remote _ ADDR];} else {$server = '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 is available all over the Internet.
The code for the modifyipcount () function 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 ($daylighting UPDATE count SET day=' row1 ['day']) {$query= "UPDATE count SET day='". $day. "', todayipcount='1'"; mysql_query ($query);}
Note: here I set the number of visits within 24 hours to add only 1.
This is the end of the content of "how php achieves the number of visitors". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!