访客计数器是让 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. be
| 对此文章发表了评论 |
