How to achieve the number of visitors in php

藏色散人
Release: 2023-03-14 12:26:02
Original
2575 people have browsed it

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));
Copy after login

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');
Copy after login

2, implementation method

In your case Put the following code on the page that counts the number of times:

$realip=getip();
modifyipcount($realip);
Copy after login

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;
}
Copy after login

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);
                }        
        }
}
Copy after login

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!

Related labels:
php
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!