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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <?php $file = dirname(__FILE__).'/tongji.txt';
$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 "总访问量:{$total}, 本月访问量:{$month}, 昨日访问量:{$yesterday}, 今日访问量:{$today}"; } ?>
|