本款計數(shù)器用文本計數(shù),沒有用到數(shù)據(jù)庫,可以實現(xiàn)如下功能:
利用一個文本文件實現(xiàn)多個頁的計數(shù)
減少服務(wù)器的i/o占用率
在需要紀(jì)錄的文件里,只需加入很少的幾行代碼
基本思路如下:
服務(wù)器程序從文本文件中讀取該頁被瀏覽的次數(shù),
(因為所有文件向服務(wù)器提出請求時,他們的環(huán)境變量request_uri都代表他來自于...
所以,以請求文件的環(huán)境變量request_uri來辨別到底是那一頁正被瀏覽。)
將這個次數(shù)加一儲存,并在調(diào)用這頁的用戶的計算機上顯示出來。
請先看我的數(shù)據(jù)文本中紀(jì)錄的數(shù)據(jù)樣本,(紅色為瀏覽次數(shù),其前面為相應(yīng)的被瀏覽的文件)
counter.dat |
好! 我們來看看php文件
counter.php
<html>
<head>
<title>計數(shù)器</title>
<head>
<body>
<?php
/* 定義儲存數(shù)據(jù)的文本文件 */
$counterfile="counter.dat";
/* 定義一個標(biāo)記,用來辨別現(xiàn)在需紀(jì)錄的數(shù)據(jù)是否已經(jīng)文本數(shù)據(jù)中 */
$sign=false;
/* 將數(shù)據(jù)以數(shù)組的方式讀入變量 $sounterdata 備用, */
$counterdata=file($counterfile);
/* 用count()函數(shù)計算共有多少個紀(jì)錄 */
/* 用explode()函數(shù)把$counterdata[$i]按符號"|"分開,并以數(shù)組的方式送回到變量$vararray里 */
/* 函數(shù)implode()與explode()剛剛相反,把數(shù)組$vararray以符號"|"連接起來送回到$counterdata */
/* 還利用了環(huán)境變量$path_info
for($i=0;$i<=count($counterdata)-1;$i++)
{
$vararray=explode("|",$counterdata[$i]);
if ($vararray[0]==$globals["request_uri"])
{
$vararray[1]++;
print($vararray[1]);
$counterdata[$i]=implode("|", $vararray);
$sign=true;
/* 找到本紀(jì)錄所在的位置后, 用break 退出循環(huán) */
break;
}
}
/* 在這里,利用implode()這個函數(shù)的功能,將數(shù)據(jù)整理好了,一起寫入文本文件中 */
/* 這樣,對服務(wù)器的i/o占用就降到了最低點
$data=implode("",$counterdata);
/* 打開文本文件,將數(shù)據(jù)寫入 */
$fp=fopen($counterfile,"w");
fputs($fp,$data);
/* 如果需要紀(jì)錄的數(shù)據(jù)不在文本里,標(biāo)志$sign= flase, 那么就往文本里添加數(shù)據(jù) */
if (!$sign) {fputs($fp,"/n".$globals["request_uri"]."|"."1"."|");
print("1");
/* 關(guān)閉數(shù)據(jù)文件 */
fclose($fp);
?>
</body>
</html>
我們已經(jīng)看到了這段程序的工作過程,也都知道了思路,但如果,每個文件里都這樣寫,豈不是太麻煩.
別慌! 我們還有php提供的強大的require()功能呢! 我們把counter.php寫成函數(shù),使用上不就方便了.
要想使用require()功能,您必須在php.ini里做出相應(yīng)的配置.
不妨參考一下我的配置過程:
(看過我的"成功之路"的朋友要注意了,我對httpd.conf做了小小的修改,希望對照)
httpd.conf 的配置的相關(guān)部分是: scriptalias /php4/ "c:/php4/" 為了不至于搞混,普通的 *.php 文件,放置在c:/php4/script下 那么,php.ini里該咋樣配置呢? 這反而簡單: 在php.ini里尋找"include_path",將它改為: include_path ="c:/php4/script/include" 即可 |
counter.inc
<?php
function counter()
{
$counterfile="c://php4//script//counter.dat";
$counterdata=file($counterfile);
$sign=false;
for($i=0;$i<=count($counterdata)-1;$i++)
{
$vararray=explode("|",$counterdata[$i]);
if ($vararray[0]==$globals["request_uri"])
{
$vararray[1]++;
print($vararray[1]);
$counterdata[$i]=implode("|", $vararray);
$sign=true; break;
}
}
$data=implode("",$counterdata);
$fp=fopen($counterfile,"w");
fputs($fp,$data);
if (!$sign)
{
fputs($fp,"/n".$globals["request_uri"]."|"."1"."|");
print("1");
}
fclose($fp);
}
?>
將他存放在c:/php4/script/include/下,即:c:/php4/script/include/counter.inc
細(xì)心的您肯定發(fā)現(xiàn)他們有不同之處:$counterfile="c://php4//script//counter.dat"
沒錯,為了所有的文件都放置在一個地方,必須提供絕對路徑. unix 有所不同喔.
好了來看我們怎樣調(diào)用它,先看一個例子:
countertest.php
<?php
require("counter.inc");
?>
<html>
<head>
<title> 網(wǎng)頁計數(shù)器 終結(jié)版 </title>
</head>
<body>
.
.
.
您是第<? counter();?>位閱讀者
.
.
.
</body>
</html>
您只需在要計數(shù)的html文件的文件頭加入require()函數(shù),引入counter()函數(shù)為homepage的一部分.
在需要的地方加入<? counter();?>就可以了.
最大的網(wǎng)站源碼資源下載站,
新聞熱點
疑難解答