实现功能:文章浏览量的统计显示

实现的详细:对文章的总浏览量、当天浏览量、当周浏览量、当月浏览量进行统计

实现代码位置:coreframe/app/content/stat.php

实现的详细代码

 <?php
// +----------------------------------------------------------------------
/**
* 内容模块访问统计
*/
defined('IN_WZ') or exit('No direct script access allowed');
$db = load_class('db');
$formdata = array();
$formdata['id'] = isset($GLOBALS['id']) ? intval($GLOBALS['id']) : exit('-1');
$formdata['cid'] = isset($GLOBALS['cid']) ? intval($GLOBALS['cid']) : exit('-2');
if(!$formdata['id'] || !$formdata['cid']) exit('-3');
$formdata['addtime'] = SYS_TIME;
$formdata['ip'] = get_ip(); $qkey = get_cookie('qkey');
if($qkey=='') {
$formdata['qkey'] = uniqid();//13位 唯一值,从cookie中获取和写入,用于记录uv和pv
$lefttime = SYS_TIME+2592000;
set_cookie('qkey',$formdata['qkey'],$lefttime);
} else {
$formdata['qkey'] = $qkey;
} $db->insert('content_stat',$formdata); $r = $db->get_one('content_rank',array('cid'=>$formdata['cid'],'id'=>$formdata['id']));
if(!$r) {
$db->insert('content_rank', array('cid'=>$formdata['cid'],'id'=>$formdata['id']));
$r['views'] = 0;
}
$views = $r['views'] + 1;
$yesterdayviews = (date('Ymd', $r['updatetime']) == date('Ymd', strtotime('-1 day'))) ? $r['dayviews'] : $r['yesterdayviews'];
$dayviews = (date('Ymd', $r['updatetime']) == date('Ymd', SYS_TIME)) ? ($r['dayviews'] + 1) : 1;
$weekviews = (date('YW', $r['updatetime']) == date('YW', SYS_TIME)) ? ($r['weekviews'] + 1) : 1;
$monthviews = (date('Ym', $r['updatetime']) == date('Ym', SYS_TIME)) ? ($r['monthviews'] + 1) : 1;
$db_array = array('views'=>$views,'yesterdayviews'=>$yesterdayviews,'dayviews'=>$dayviews,'weekviews'=>$weekviews,'monthviews'=>$monthviews,'updatetime'=>SYS_TIME); $db->update('content_rank',$db_array, array('cid'=>$formdata['cid'],'id'=>$formdata['id'])); echo '$("#hits").html('.$views.')';
?>

数据库结构:

DROP TABLE IF EXISTS `wz_content_rank`;
CREATE TABLE `wz_content_rank` (
`cid` mediumint(10) unsigned NOT NULL,
`id` int(10) unsigned NOT NULL DEFAULT '',
`views` int(10) unsigned NOT NULL DEFAULT '',
`yesterdayviews` int(10) unsigned NOT NULL DEFAULT '',
`dayviews` int(10) unsigned NOT NULL DEFAULT '',
`weekviews` int(10) unsigned NOT NULL DEFAULT '',
`monthviews` int(10) unsigned NOT NULL DEFAULT '',
`updatetime` int(10) unsigned NOT NULL DEFAULT '',
UNIQUE KEY `cid` (`cid`,`id`),
KEY `views` (`views`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

加载方式:

内容页面调用js代码

<script type="text/javascript" src="{WEBURL}index.php?f=stat.php&id={$id}&cid={$cid}"></script>

显示文章浏览量

<span id="hit">此处显示浏览量</span>

注:页面中需要引入jQuery框架。

05-08 15:12