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
| 对此文章发表了评论 |
