[PHP] 瀏覽人數計算

雖然有用Google Analytics,但及時資料的API還在Beta中,所以還是索性寫了一個簡易的計算瀏覽人數的程式。

簡單來說就是計數器而已。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
$totalfile = fopen("total.txt", "r");
$total = fgets($totalfile);
$total ++ ;
fclose($totalfile);
$totalfile = fopen("total.txt", "w");
fwrite($totalfile,$total) ;
fclose($totalfile);
$date = date("Y-m-d",filemtime("today.txt")) ;
$todayfile = fopen("today.txt", "r");
if ( $date !== date ("Y-m-d")){
$today = 0 ;
} else {
$today = fgets($todayfile);
}
$today ++ ;
fclose($todayfile);
$todayfile = fopen("today.txt", "w");
fwrite($todayfile,$today) ;
fclose($todayfile);
echo $total ;
echo $today ;
?>