<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>SD工作室:PHP教學範例-簡易文字訪客計數器</title>
</head>
<body bgcolor="#FFFFCC">
<body>
<?php
/*
在讀取到本頁時,php 程式先建立一個 type01_counter.txt檔案,
然後將1寫入檔案。
在$counter_num的變數出現在瀏覽器前,
經過加一的動作,讓瀏覽訪客人數可以增加。
最後將訪客人數再回存type01_counter.txt檔案
[缺點]
當使用者瀏覽網頁時,人數就會加1,
人數的資料會記錄在type01_counter文字檔中。
訪客者按了"重新整理,若還是同一個訪客者,
訪客人數亦會隨之增加。
*/
$n=4; //變量$n是顯示位數
if (file_exists("type01_counter.txt"))
{
//只讀方式打開type01_counter.txt文件
$counterfile=fopen("type01_counter.txt","r");
//讀取4位數字
$counter_num=fgets($counterfile,$n+1);
//瀏覽次數加一
$counter_num++;
//關閉文件
fclose($counterfile);
}
else
{
$counter_num=1;
}
//只寫方式打開type01_counter.txt文件
$counterfile = fopen("type01_counter.txt","w");
//寫入加一後結果
fputs($counterfile,$counter_num);
//關閉文件
fclose($counterfile);
//瀏覽器輸出瀏覽次數
echo "<font color='purple'><b>您是第 ".$counter_num . "位訪客</font>";
?>
</body>
</html>
留言列表