访客计数器是让 web 访客知道该网页或者网站的人气指数最直接的方法。尤其是想利用网页赚钱的人,访客人数是找广告商最好的说词。当然可以将网站来访人数写成统计报表,但总是感觉直接看到比较真实,到底眼见为凭。
在上图中,访客计数器的流程如下
- 第一位用户浏览某页。
- 服务器程序从数据库或文件中读取该页被浏览次数。
- 将次数加一储存,并将它送回第一位用户。
- 第二位用户浏览某页。
- 服务器程序从数据库或文件中读取该页被浏览次数。
- 将次数再加一储存,并将它送回第二位用户。
php 在没有特殊的访客计数器函数,但是我们可以用 php 的强大功能自已写一个访客计数器函数。
以下的函数是访客计数器的原型,是由 david w. bettis 所提供,并经过作者少许修改。
<html>
<head>
<title>访客计数器 原型</title>
</head>
<body>
<?php
/*
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 );
?>
</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
//---------------------------
// 访客计数器函数 mycounter()
// author: wilson peng
// copyright (c) 1999
//---------------------------
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" ) ;
最后将计数器文件删除,再重新建立一个。就完成了这个以文件为基础的纯文字计数器。