php method to implement single article browsing count: 1. Create a php sample file; 2. Obtain the IP through "$_SERVER['REMOTE_ADDR'];"; 3. Through "if(mysql_num_rows($query ) > 0){...}" circularly compares the IPs stored in the data table. If the IP exists, it will not be recorded again.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
How to achieve single article view count in php?
PHP counts the number of visits to a single article
First add a field to record the number of visits to the data table that stores the corresponding article.
You can add one to the number of fields each time you access it, but this will be inaccurate because it will be recorded as long as it is refreshed.
In order to prevent the above situation from happening, you can record the IP address of the visitor. Repeated IP address visits will only be recorded once.
However, there are problems if an IP address is only recorded once. For example, if multiple visitors on a local area network share the same IP address, the records will be inaccurate. Therefore, it is best to record one visit to an IP address once within a certain period of time. This is relatively better.
However, even if the IP address is recorded, there may be malicious users who switch IPs or use ticket brushing software to increase the number of visits.
<?php $ip = $_SERVER['REMOTE_ADDR']; //循环对比数据表中储存的IP,如果IP存在,不再记录 $query = mysql_query("select * contetns where ip = '".$ip."'"); if(mysql_num_rows($query) > 0){ //IP存在 }else{ //记录访问量 } ?>
Note: If you want to use files without using a database, you can directly write a script on the server to count the access log
Recommended learning: "PHP Video Tutorial 》
The above is the detailed content of How to achieve single article view count in php. For more information, please follow other related articles on the PHP Chinese website!