How to achieve single article view count in php

藏色散人
Release: 2023-03-17 13:44:02
Original
2048 people have browsed it

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.

How to achieve single article view count in php

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[&#39;REMOTE_ADDR&#39;];
//循环对比数据表中储存的IP,如果IP存在,不再记录
$query = mysql_query("select * contetns where ip = &#39;".$ip."&#39;");
if(mysql_num_rows($query) > 0){
//IP存在
}else{
//记录访问量
}
?>
Copy after login

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!

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