Home > CMS Tutorial > WordPress > body text

How to implement visitor statistics in WordPress

藏色散人
Release: 2020-12-29 16:55:06
forward
4456 people have browsed it

#WordPress Tutorial

How to implement visitor statistics in WordPress#WordPress Tutorial


, I hope it will be helpful to friends in need!


How to implement visitor statistics in WordPresswordpress
Program modification

A brief introduction to using php mysql to implement simple visitor statistics
How to implement visitor statistics in WordPress1.php Script

<?php 
//连接数据库 
$conn=mysql_connect("localhost","root","root"); 
if(!$conn){ 
die("链接失败".mysql_errno()); 
} 
//设置数据库编码方式 
mysql_query("set names utf8",$conn) or die(mysql_errno()); 
//选择数据库 
mysql_select_db("wordpress",$conn) or die(mysql_errno()); 
$adress=$_SERVER["REMOTE_ADDR"]; 
//将本次访客的ip地址添加到数据库中 
$sql="select times from wp_count where ip=&#39;$adress&#39;"; 
$res=mysql_query($sql,$conn); 
if(!$row=mysql_fetch_row($res)){ 
$sql="insert into wp_count(ip, times) values(&#39;$adress&#39;,&#39;1&#39;)"; 
}else{ 
$times = $row[&#39;0&#39;]+1; 
$sql="update wp_count set times=&#39;$times&#39; where ip=&#39;$adress&#39;"; 
} 
$res=mysql_query($sql,$conn);
//发送语句获取总数 
$sql="select count(ip) from wp_count"; 
$res=mysql_query($sql,$conn); 
if($row=mysql_fetch_row($res)){ 
$num=$row[&#39;0&#39;]; 
} 
echo"您是第 "."$num"." 位访客"."您的ip地址是"."$adress"; 
mysql_close(); 
?>
Copy after login

Explanation: When the current IP does not exist in the database, insert is executed. Otherwise execute update. insert inserts ip and initial value times=1, update updates times 1.

2. Database example

Table wp_count

  ps: The above table structure meets my needs, because my site counts the number of IPs , and I regard the same IP as a visitor no matter how many times it is accessed. The number of digits displayed on the page is the total number of IP visits. ######3. Effect ###  ######4. Extension######The above example is implemented using a database. Of course, it is also possible to use txt text. ######Based on simple visitor statistics, you can add a very accurate site statistics to your site. For example, to realize the visitor's last visit time, you can use js to trigger the modification of the visit time when the visitor closes the current page. ######ps: After testing, Baidu statistics are not very accurate. Because Baidu uses referenced js for data statistics, if the site js fails to load or for other reasons, some data may not be counted. #########

The above is the detailed content of How to implement visitor statistics in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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