php学习笔记(3)
1、计数器
<?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); ?>
<?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"); } ?>
当然,要用的话要加 Homepage 中嵌入 MyCounter() 函数,就可以使用了
<?php require("counter.inc"); ?> <html> <head> <title>访客计数器 最终版</title> </head> <body> 您是第 <? MyCounter(); ?> 位参观者 </body> </html>
要用这个 MyCounter() 函数,先在 Homepage 的开头处加入 require() 函数,引入 MyCounter() 函数成为该 Homepage 的一部份。之后再将 <? MyCounter(); ?> 字符串放在需要计数器的地方就可以了。

浙公网安备 33010602011771号