$file = dirname(__FILE__).'/tongji.db';
//$data = unserialize(file_get_contents($file));
$fp=fopen($file,'r+');
$content='';
if (flock($fp,LOCK_EX)){
while (($buffer=fgets($fp,1024))!=false){
$content=$content.$buffer;
}
$data=unserialize($content);
//設置記錄鍵值
$total = 'total';
$month = date('Ym');
$today = date('Ymd');
$yesterday = date('Ymd',strtotime("-1 day"));
$tongji = array();
// 總訪問增加
$tongji[$total] = $data[$total] + 1;
// 本月訪問量增加
$tongji[$month] = $data[$month] + 1;
// 今日訪問增加
$tongji[$today] = $data[$today] + 1;
//保持昨天訪問
$tongji[$yesterday] = $data[$yesterday];
//保存統計數據
ftruncate($fp,0); // 將文件截斷到給定的長度
rewind($fp); // 倒回文件指針的位置
fwrite($fp, serialize($tongji));
flock($fp,LOCK_UN);
fclose($fp);
//輸出數據
$total = $tongji[$total];
$month = $tongji[$month];
$today = $tongji[$today];
$yesterday = $tongji[$yesterday]?$tongji[$yesterday]:0;
echo "document.write('訪總問 {$total}, 本月 {$month}, 昨日 {$yesterday}, 今日 {$today}');";
}
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP目錄操作技巧匯總》、《php文件操作總結》、《PHP常用遍歷算法與技巧總結》、《PHP數據結構與算法教程》、《php程序設計算法總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》及《php常見數據庫操作技巧匯總》