麻辣家族討論版版's Archiver

小誌 發表於 2010-5-16 14:32

(PHP+IIS+MsSQL教學第22篇) 資料儲存與檔案管理 下篇(實作範例)

[color=DarkOrange][size=5][b]網頁計數器[/b][/size][/color]
Session物件的生命週期起始於PWS/IIS 開始運作且有人開始連線時。終止於 PWS/IIS 關閉,或客戶端瀏覽器未於特定時間內再向伺服器要求資訊,所以Session物件物件並不是永遠存在的!況且,對於所有的連線瀏覽者而言,都個別擁有一個『私用』的 Session 物件,所以,Session物件並不適合用於建立計數器。


Cookie物件將資訊建立於客戶端瀏覽器的,更不適合於計數器;反觀檔案式的計數器乃利用檔案的存取來記錄計數資料,也就是說:我們的計數器計次資料將會被儲存在一個特定的資料檔案中,計數資料的保存並不會因時間的改變而消失,或是因為伺服器進行維護而使計數器計次資料被歸零。


為了使我們辛苦累積的瀏覽人次計數器資料不至於人間蒸發,所以我們必須利用檔案資料處理函數的優勢與特點,統合運用來設計我們的計數器,下例即為統合檔案資料處理函數來製作計數器的程式網頁內容:
txtcount.php[code]<?
$countfile="count.txt";
global $num;
if (file_exists($countfile))
   {
    $file=fopen($countfile,"r");
    $num=fgets($file,filesize($countfile)+1);
    $num++;
    fclose($file);
   }
else
   {
    $num="1";
   }
$file=fopen($countfile,"w");
fwrite($file,$num);
fclose($file);
?>

<html>
<head>
<meta http-equiv="Content-Language" content="zh-tw">
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>簡單實用的文字計數器</title>
</head>
<body>
<p align="center"><font size="5">簡單實用的文字計數器</font></p>
<hr>
<p align="center"><b><font color="#FF0000">
參觀人次:<?=$num?></font></b></p>
</body>
</html>[/code][list]
[*]程式碼第4行:當一有新的瀏覽者連結進入網頁開始瀏覽時就先執行『檔案檢驗』這行PHP程式敘述,利用file_exists()函數檢驗檔案是否存在,如此一來,可避免計數資料檔不存在而造成程式的失誤。
[*]程式碼第6~9行:當計數資料檔存在時,則利用fopen()函數開啟計數資料檔,接著使用fgets()函數取得計次次數資料,將繼次資料+1後,就是最新的計次次數資料。
[*]程式碼第11~14行:當計數資料檔存在時,代表網頁尚未有人瀏覽,此時的瀏覽者即為第1人。
[*]程式碼第15~17行:利用fopen()函數開啟或建立自動計數資料檔為寫入狀態,將最新的計次次數資料透過fwrite()函數寫入檔案中保存。
[/list]
[attach]476[/attach]


[color=Red][b]補充:[/b][/color]
filesize()函數:取得檔案的大小。
filesize()函數格式[code]int filesize ( string filename)[/code]若有錯誤發生回傳False,否則回傳檔案大小(bytes)。


is_readable()函數:判斷檔案能否讀取。
is_readable()函數格式[code]bool is_readable ( string filename)[/code]若檔案可讀取資料(非唯寫),則回傳Trrue,否則False。

is_readable()函數:判斷檔案能否讀取。
is_writeable()函數格式[code]bool is_writeable ( string filename)[/code]若檔案可寫入資料(非唯讀),則回傳Trrue,否則False。

小誌 發表於 2010-5-16 14:40

[color=DarkOrange][size=5][b]圖形計數器[/b][/size][/color]
文字型計數器看起來好像單調了一點,我們何不將它改成圖片式的圖形計數器,日後如果想換換口味的話,直接將圖檔換掉就行了!
[attach]478[/attach]

在撰寫程式之前,請先準備好代表0~9的數字圖片,檔名跟圖片的內容數字必須相符:
[attach]479[/attach]                  
為了將計數檔案內的資料取出來並轉換為圖片,我們必須將數字的每一個「字」拆解出來,並以圖片檔名替代:[code]<?
function picout($num)
{
$strlen=strlen($num);
for ($x=0;$x<$strlen;$x++)
{
$picnum=substr($num,$x,1);
echo "<img src=$picnum.gif>";
}
}
?>[/code][list]
[*]程式碼第4行:先利用strlen()函數計算字串的長度(計數資料的位數)。
[*]程式碼第5~9行:使用for迴圈將各個圖片檔的HTML標籤敘述輸出。
[*]利用substr()函數拆解字串,每次取出一個字元。
[/list]
為了不變動原始的文字計數器功能,所以小誌把文字轉換成圖片HTML標籤敘述的程式區塊用函數包起來,所以我們只要將本來輸出文字資料的地方改成呼叫副程式即可。
piccount.php[code]<?
$countfile="piccount.txt";
global $num;
if (file_exists($countfile))
   {
    $file=fopen($countfile,"r");
    $num=fgets($file,filesize($countfile)+1);
    $num++;
    fclose($file);
   }
else
   {
    $num="1";
   }
$file=fopen($countfile,"w");
fwrite($file,$num);
fclose($file);
?>

<html>
<head>
<meta http-equiv="Content-Language" content="zh-tw">
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>實用的圖片計數器</title>
</head>
<body>
<p align="center"><font size="5">實用的圖片字計數器</font></p>
<hr>
<p align="center"><b><font color="#FF0000">
參觀人次:<?=picout($num)?></font></b></p>
</body>
</html>

<?
function picout($num)
{
$strlen=strlen($num);
for ($x=0;$x<$strlen;$x++)
  {
   $picnum=substr($num,$x,1);
    echo "<img src=$picnum.gif>";
  }
}
?>[/code][color=Red]另解:[/color]Piccount_2.php[code]<?
$countfile="piccount.txt";
if (file_exists($countfile))
   {
    $file=fopen($countfile,"r");
    $num=fread($file,filesize($countfile));
    $num++;
    fclose($file);
    $file=fopen($countfile,"w");
    fwrite($file,$num);
    fclose($file);
   }
else
   {
    $num=1;
    $file=fopen($countfile,"w");
    fwrite($file,$num);
    fclose($file);
   }
?>

<html>
<head>
<meta http-equiv="Content-Language" content="zh-tw">
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>實用的圖片計數器</title>
</head>
<body>
<p align="center"><font size="5">實用的圖片字計數器</font></p>
<hr>
<p align="center"><b><font color="#FF0000">
參觀人次:<?=picout()?></font></b></p>
</body>
</html>

<?
function picout()
{
global $countfile;
$file=fopen($countfile,"r");
while (false != ($char = fgetc($file)))
  {
     echo "<img src=" . $char . ".gif>";
  }
fclose($file);
}
?>[/code]圖形計數器完整原始碼下載:
[attach]480[/attach]

頁: [1]

麻辣家族討論版版為 麻辣學園 網站成員  由 昱得資訊工作室 © Since 1993 所提供