首頁  >  文章  >  php教程  >  文件記錄網頁瀏覽量

文件記錄網頁瀏覽量

WBOY
WBOY原創
2016-09-06 08:42:431823瀏覽

檔案記錄網頁瀏覽量,高併發使用正常;
$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}');";

}

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn