实现 总访问 , 本月访问, 昨日访问, 今日访问统计,使用文本存储,简单好用
自动创建文本数据库,保存本php同目录。显示效果如图:
调用方法:
<?php <br />
error_reporting(0);<br>
header ( "Content-type: text/html; charset=utf-8" );<br>
//设置数据文件路径<br>
$file = dirname(__FILE__).'/tongji.db';<br>
$data = unserialize(file_get_contents($file));<br>
//设置记录键值<br>
$total = 'total';<br>
$month = date('Ym');<br>
$today = date('Ymd');<br>
$yesterday = date('Ymd',strtotime("-1 day"));<br>
$tongji = array();<br>
// 总访问增加<br>
$tongji[$total] = $data[$total] + 1;<br>
// 本月访问量增加<br>
$tongji[$month] = $data[$month] + 1;<br>
// 今日访问增加<br>
$tongji[$today] = $data[$today] + 1;<br>
//保持昨天访问<br>
$tongji[$yesterday] = $data[$yesterday];<br>
//保存统计数据<br>
file_put_contents($file, serialize($tongji));<br>
//输出数据<br>
$total = $tongji[$total];<br>
$month = $tongji[$month];<br>
$today = $tongji[$today];<br>
$yesterday = $tongji[$yesterday]?$tongji[$yesterday]:0;<br>
echo "document.write('总访问 {$total}, 本月 {$month}, 昨日 {$yesterday}, 今日 {$today}');";
云栖大会北京站:阿里技术专家难得出镜,这次一下来了100多位?!