原 PO 問題如下
※ 引述《gyd (ipod touch之路)》之銘言:
: 以下是計數器的程式碼
: $c=file("counter.txt");
: $visit=$c[0]+1;
: echo "$visit";
: $fp=fopen("counter.txt", "w");
想請問一下
每過一段時間後我都會發現計數器又從0開始算
是code有bug還是?
請問如何解決或是有更佳的程式寫法?
bibo9901 的回答
問題在這個"w"
根據php.net
Open for writing only;place the file pointer at the beginning of the file
and truncate the file to zero length.
If the file does not exist, attempt to create it.
就是說,當執行到 $fp=fopen("counter.txt","w"); 之後
counter.txt 會被清空
這時如果有另一人執行同樣的 php
$c=file("counter") 會變成空的陣列
所以 fputs($fp,$c[0]+1); 會將 1 寫入 counter.txt
看起來就好像是歸零一樣XD
難怪以前我的計數器都會被歸 0
全站熱搜