PHP网页计数器的原理

80酷酷网    80kuku.com

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


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

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

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


<html>
<head>
<title>访客计数器 原型</title>
</head>
<body>
<?php
/*
simple access counter for php3
(c)1998 David W. Bettis
dbettiseyeintegrated.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);

?>
</body>
</html>


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 所设定的计数器中的变数功能相同。


<?php
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() 函式,就可以使用了。


<?php
require("counter.inc");
?>
<html>
<head>
<title>访客计数器 最终版</title>
</head>
<body>
您是第 <? MyCounter(); ?> 位参观者
</body>
</html>


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");

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

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: