Home  >  Article  >  Backend Development  >  How to achieve the number of visitors in php

How to achieve the number of visitors in php

藏色散人
藏色散人Original
2021-12-13 09:39:282608browse

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. .

How to achieve the number of visitors in php

#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=&#39;".$ip."&#39;";
        $result=mysql_query($query);
        $row=mysql_fetch_array($result);
        $iptime=time();
        $day=date(&#39;j&#39;);
        if(!$row)
        {
                $query="INSERT INTO ip (ipdata,iptime) VALUES (&#39;".$ip."&#39;,&#39;".$iptime."&#39;)";
                mysql_query($query);
                $query="SELECT day,todayipcount,allipcount FROM count";
                $result=mysql_query($query);
                $row=mysql_fetch_array($result);
                $allipcount=$row[&#39;allipcount&#39;]+1;
                $todayipcount=$row[&#39;todayipcount&#39;]+1;
                if($day==$row[&#39;day&#39;])
                {
                        $query="UPDATE count SET allipcount=&#39;".$allipcount."&#39;,todayipcount=&#39;".$todayipcount."&#39;";
                }
                else
                {
                        $query="UPDATE count SET allipcount=&#39;".$allipcount."&#39;,day=&#39;".$day."&#39;,todayipcount=&#39;1&#39;";
                }
                 mysql_query($query);
        }
        else
        {
                $query="SELECT iptime FROM ip WHERE ipdata=&#39;".$ip."&#39;";
                $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[&#39;iptime&#39;]>86400)
                {
                                                $query="UPDATE ip SET iptime=&#39;".$iptime."&#39; WHERE ipdata=&#39;".$ip."&#39;";
                 mysql_query($query);
                        $allipcount=$row1[&#39;allipcount&#39;]+1;
                        if($day==$row1[&#39;day&#39;])
                        {
                                $query="UPDATE count SET allipcount=&#39;".$allipcount."&#39;";
                        }
                        else
                        {
                                $query="UPDATE count SET allipcount=&#39;".$allipcount."&#39;,day=&#39;".$day."&#39;,todayipcount=&#39;1&#39;";
                        }
                         mysql_query($query);
                }
                if($day!=$row1[&#39;day&#39;])
                {
                        $query="UPDATE count SET day=&#39;".$day."&#39;,todayipcount=&#39;1&#39;";
                         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!

Statement:
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