PHP网站统计简易版本,文本数据库

浏览:2140 发布日期:2016/06/29 分类:功能实现
实现 总访问 , 本月访问, 昨日访问, 今日访问统计,使用文本存储,简单好用
自动创建文本数据库,保存本php同目录。显示效果如图:

调用方法:
<script language="javascript" type="text/javascript" src="tongji.php"></script><?php
error_reporting(0);
header ( "Content-type: text/html; charset=utf-8" );
//设置数据文件路径
$file = dirname(__FILE__).'/tongji.db';
$data = unserialize(file_get_contents($file));
//设置记录键值
$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];
//保存统计数据
file_put_contents($file, serialize($tongji));
//输出数据
$total = $tongji[$total];
$month = $tongji[$month];
$today = $tongji[$today];
$yesterday = $tongji[$yesterday]?$tongji[$yesterday]:0;
echo  "document.write('总访问 {$total}, 本月 {$month}, 昨日 {$yesterday}, 今日 {$today}');";
评论( 相关
后面还有条评论,点击查看>>