Home php教程 php手册 PHP网页计数器的原理

PHP网页计数器的原理

Jun 21, 2016 am 08:58 AM
counter gt

计数器|网页

  访客计数器是让 Web 访客知道该网页或者网站的人气指数最直接的方法。尤其是想利用网页赚钱的人,访客人数是找广告商最好的说词。当然可以将网站来访人数写成统计报表,但总是感觉直接看到比较真实,到底眼见为凭。


在上图中,访客计数器的流程如下

1.. 第一位使用者浏览某页。
2.. 伺服器程式从资料库或档案中读取该页被浏览次数。
3.. 将次数加一储存,并将它送回第一位使用者。
4.. 第二位使用者浏览某页。
5.. 伺服器程式从资料库或档案中读取该页被浏览次数。
6.. 将次数再加一储存,并将它送回第二位使用者。
PHP 在没有特殊的访客计数器函式,但是我们可以用 PHP 的强大功能自已写一个访客计数器函式。

以下的函式是访客计数器的原型,是由 David W. Bettis 所提供,并经过作者少许修改。




访客计数器 原型


/*
simple access counter for php3
(c)1998 David W. Bettis
dbettis@eyeintegrated.com
medify by Wilson Peng
*/

$counterFile = "/tmp/counter.txt";

function displayCounter($counterFile) {
$fp = fopen($counterFile,"rw");
$num = fgets($fp,5);
$num += 1;
print "您是第 "."$num"." 位无聊份子";
exec( "rm -rf $counterFile");
exec( "echo $num > $counterFile");
}

if (!file_exists($counterFile)) {
exec( "echo 0 > $counterFile");
}

displayCounter($counterFile);

?>




Copyright ? 1998 David W. Bettis

在读取到本页时,PHP 程式先找寻 /tmp/counter.txt 档案是否存在,若不存在,则建立一个 counter.txt 档案,然后将 0 写入档案。然后读取 counter.txt 档案的内容,也就是纯文字档,再将内文的数字存入 $num 变数中。在 $num 的变数出现在浏览器前,还有经过加一的动作,让使用者可以增加。当然,如果想灌水,就在加一动作时加二或者加三,不过自欺是无用的。最后将访客人数再回存 /tmp/counter.txt
就一切 OK。

当然,每一页都要这样写,岂不麻烦到了极点。这时,我们可以利用 PHP 提供的 require() 功能,将计数器整理成一个函式,酱子在使用上就方便多多了。

首先要先将 Apache 的设定档 (httpd.conf) 加入 PHP include 档案的路径。例如要设所有的 include 档都在 http://abcdefghijk.com.tw/include 中,可以在httpd.conf 加入下面的例子

php3_include_path .:./include:../include

别忘了重新启动 Apache 伺服器,新增的 include 路径才有效。

./apachectl restart

再来就在伺服器的 .../include 目录中放入以下的档案,档名存成
counter.inc

下面就是 MyCounter() 函式。为了让读者方便了解,程式中的变数
$counterFile、$fp 及 $num 保持和 David W. Bettis 所设定的计数器中的变数功能相同。


file://---------------------------
// 访客计数器函式 MyCounter()
// Author: Wilson Peng
// Copyright (C) 1999
file://---------------------------
function MyCounter() {
$counterFile="/tmp".$GLOBALS["PHP_SELF"];
if (!file_exists($counterFile)) {
if (!file_exists(dirname($counterFile))) {
mkdir(dirname($counterFile), 0700);
}
exec("echo 0 > $counterFile");
}
$fp = fopen($counterFile,"rw");
$num = fgets($fp,5);
$num += 1;
print "$num";
echo $counterFile;
exec("rm -rf $counterFile");
exec("echo $num > $counterFile");
}
?>


Copyright ? 1999, Wilson Peng

当然,要用的话要加 Homepage 中嵌入 MyCounter() 函式,就可以使用了。


require("counter.inc");
?>


访客计数器 最终版


您是第 MyCounter(); ?> 位参观者




Copyright ? 1999, Wilson Peng

要用这个 MyCounter() 函式,先在 Homepage 的开头处加入 require() 函式,引入 MyCounter() 函式成为该 Homepage 的一部份。之后再将 MyCounter(); ?>字串放在需要计数器的地方就可以了。

function MyCounter() {
:
:

}
在建立函式时,需要用上面的格式。在自订函式名称前加入 function 字串。

每页有用到 MyCounter() 的 Homepage 都会在 /tmp 之后加入该页的路径,这可以用 $PHP_SELF 变数达成。

$counterFile="/tmp".$GLOBALS["PHP_SELF"];

当然,若您要将 /tmp 改成别的目录也可以,不然在 SUN 等伺服器,要是reboot,/tmp 中的东西都没了,要重新开始再计数了。若您不知要使用什么目录,建议使用 /var/log/counter 这个目录,和其它的 log 等变动资料放在一起。

if (!file_exists($counterFile)) {
if (!file_exists(dirname($counterFile))) {
mkdir(dirname($counterFile), 0700);
}
exec("echo 0 > $counterFile");
}

这五行主要是检查 $counterFile 是否存在,若档案不存在则看目录是否存在,决定要不要建立目录。之后就建立档案,并写入 0。

$fp = fopen($counterFile,"rw");
$num = fgets($fp,5);
$num += 1;
print "$num";
echo $counterFile;

这五行就是打开计数器存放的档案,并将它累加后的结果送到浏览器端。

exec("rm -rf $counterFile");
exec("echo $num > $counterFile");

最后将计数器档案删除,再重新建立一个。就完成了这个以档案为基础的纯文字计数器。



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

How to use the Counter module in Python How to use the Counter module in Python Apr 19, 2023 pm 02:55 PM

Description Project Description Python Interpreter 3.10.6 Counter module In Python's collections module, a very commonly used module is Counter. Counter is a simple counter used to count the number of certain hashable objects. It stores elements and their counts in the form of a dictionary. Counter() class Counter() can count the parameters passed to this class according to certain rules, and return the results in the form of a dictionary by using the counting object and the counting result as key-value pairs. Counter(iterable=None,/,**kwds) gives an example fromcollectionsimport

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

How to optimize iPad battery life with iPadOS 17.4 How to optimize iPad battery life with iPadOS 17.4 Mar 21, 2024 pm 10:31 PM

How to Optimize iPad Battery Life with iPadOS 17.4 Extending battery life is key to the mobile device experience, and the iPad is a good example. If you feel like your iPad's battery is draining too quickly, don't worry, there are a number of tricks and tweaks in iPadOS 17.4 that can significantly extend the run time of your device. The goal of this in-depth guide is not just to provide information, but to change the way you use your iPad, enhance your overall battery management, and ensure you can rely on your device for longer without having to charge it. By adopting the practices outlined here, you take a step toward more efficient and mindful use of technology that is tailored to your individual needs and usage patterns. Identify major energy consumers

请教怎么修改url某一参数的参数值呢?是要拆开了再拼回去吗 请教怎么修改url某一参数的参数值呢?是要拆开了再拼回去吗 Jun 13, 2016 am 10:24 AM

请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?那么请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?http://127.0.0.1/myo/newuser.php?mod=search&type=fastone比如现在我要修改mod=new要怎么做呢?------解决方案--------------------发送了请求

See all articles