Comment réaliser le nombre de visiteurs en PHP : 1. Créez deux tables de base de données ; 2. Ajoutez le code "$realip=getip();modifyipcount($realip);" visiteurs.
L'environnement d'exploitation de cet article : système Windows 7, PHP version 7.4, ordinateur Dell G3.
Comment atteindre le nombre de visiteurs en php ?
PHP implémente avec précision les statistiques de visite de page
1. Deux tables de base de données sont requises
①, la table d'enregistrement 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));
Remarque : ipdata est l'adresse IP du visiteur enregistré, iptime est la visite IP enregistrée
②.
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');
$realip=getip(); modifyipcount($realip);
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; }
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); } } }
Tutoriel vidéo PHP"
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!