2.服務(wù)器讀取訪問(wèn)次數(shù)文件, 1,向客戶端返回
3.服務(wù)器保存新的瀏覽次數(shù)
4.新用戶訪問(wèn),重復(fù)123即可
解決方案(主要算法):
1.數(shù)據(jù)文件:counter.dat
2.讀出數(shù)據(jù)文件
打開(kāi)文件;
如果不存在,創(chuàng)建,并以0為初識(shí)數(shù)據(jù);
否則,讀出數(shù)據(jù);
關(guān)閉文件。
3.把累加后的數(shù)據(jù)寫(xiě)入文件counter.dat
累加數(shù)據(jù);
打開(kāi)文件;
寫(xiě)入數(shù)據(jù);
關(guān)閉文件;
4.向網(wǎng)頁(yè)輸出信息;
建立Count_Visitor文件夾。
在Count_Visitor文件夾中,創(chuàng)建Count_Visitor.php文件,并鍵入以下代碼:
<html><head> <meta charset=\”UTF-8\”> <title>瀏覽計(jì)數(shù)器-ljccccccccccc@163.com</title></head><body> <?php //數(shù)字輸出網(wǎng)頁(yè)計(jì)數(shù)器 $max_len = 9; $CounterFile = \”counter.dat\”; if(!file_exists($CounterFile)){ //如果計(jì)數(shù)器文件不存在 $counter = 0; $cf = fopen($CounterFile,\”w\”); //打開(kāi)文件 fputs($cf,\’0\’); //初始化計(jì)數(shù)器 fclose($cf); //關(guān)閉文件 } else{ //取回當(dāng)前計(jì)數(shù)器的值 $cf = fopen($CounterFile,\”r\”); $counter = trim(fgets($cf,$max_len)); fclose($cf); } $counter ; //計(jì)數(shù)器加一 $cf = fopen($CounterFile,\”w\”); //寫(xiě)入新的數(shù)據(jù) fputs($cf,$counter); fclose($cf); ?> <div id=\”dd\” align=\”center\”> <span>歡迎您!</span> <span>您是本站的第 <?php echo $counter; //輸出計(jì)數(shù)器 ?> 位訪客!</span> </div></body></html>
如果有需要可以自己鍵入css文件裝飾.
代碼中包含的變量及其含義
$max_len : 自定義變量。計(jì)數(shù)器的最大位數(shù);
$CounterFile : 自定義變量。計(jì)數(shù)器存放的文件(路徑和文件名);
$counter : 自定義變量。計(jì)數(shù)器的值;
$cf : 自定義變量。打開(kāi)計(jì)數(shù)文件的句柄;
代碼中包含的函數(shù)與含義
file_exists($CounterFile) : 判斷文件是否存在;
fopen($CounterFile,”w”) : 以寫(xiě)入方式打開(kāi)文件。(函數(shù)自帶屬性,如果不存在則創(chuàng)建)
fopen($CounterFile,\’r\’) : 以只讀方式代開(kāi)文件。
fgets($cf,$max_len) : 從文件中讀出指定長(zhǎng)度的字符
fputs($cf,”0”) : 把字符“0”寫(xiě)入$cf
fclose($cf) : 關(guān)閉$cf打開(kāi)的文件
trim(fgets($cf,$max_len)) : 去除 取出字符串的 兩邊的空格符
今天的筆記就到這啦!
很簡(jiǎn)單的php小應(yīng)用。
推薦學(xué)習(xí):《PHP視頻教程》
更多關(guān)于云服務(wù)器,域名注冊(cè),虛擬主機(jī)的問(wèn)題,請(qǐng)?jiān)L問(wèn)西部數(shù)碼官網(wǎng):m.ps-sw.cn