首页 > 代码库 > 五指cms内容浏览量实现方法

五指cms内容浏览量实现方法

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

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

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

实现的详细代码

 1 <?php
 2 // +----------------------------------------------------------------------
 3 /**
 4  * 内容模块访问统计
 5  */
 6 defined(‘IN_WZ‘) or exit(‘No direct script access allowed‘);
 7 $db = load_class(‘db‘);
 8 $formdata = array();
 9 $formdata[‘id‘] = isset($GLOBALS[‘id‘]) ? intval($GLOBALS[‘id‘]) : exit(‘-1‘);
10 $formdata[‘cid‘] = isset($GLOBALS[‘cid‘]) ? intval($GLOBALS[‘cid‘]) : exit(‘-2‘);
11 if(!$formdata[‘id‘] || !$formdata[‘cid‘]) exit(‘-3‘);
12 $formdata[‘addtime‘] = SYS_TIME;
13 $formdata[‘ip‘] = get_ip();
14 
15 $qkey = get_cookie(‘qkey‘);
16 if($qkey==‘‘) {
17     $formdata[‘qkey‘] = uniqid();//13位 唯一值,从cookie中获取和写入,用于记录uv和pv
18     $lefttime = SYS_TIME+2592000;
19     set_cookie(‘qkey‘,$formdata[‘qkey‘],$lefttime);
20 } else {
21     $formdata[‘qkey‘] = $qkey;
22 }
23 
24 $db->insert(‘content_stat‘,$formdata);
25 
26 $r = $db->get_one(‘content_rank‘,array(‘cid‘=>$formdata[‘cid‘],‘id‘=>$formdata[‘id‘]));
27 if(!$r) {
28     $db->insert(‘content_rank‘, array(‘cid‘=>$formdata[‘cid‘],‘id‘=>$formdata[‘id‘]));
29     $r[‘views‘] = 0;
30 }
31 $views = $r[‘views‘] + 1;
32 $yesterdayviews = (date(‘Ymd‘, $r[‘updatetime‘]) == date(‘Ymd‘, strtotime(‘-1 day‘))) ? $r[‘dayviews‘] : $r[‘yesterdayviews‘];
33 $dayviews = (date(‘Ymd‘, $r[‘updatetime‘]) == date(‘Ymd‘, SYS_TIME)) ? ($r[‘dayviews‘] + 1) : 1;
34 $weekviews = (date(‘YW‘, $r[‘updatetime‘]) == date(‘YW‘, SYS_TIME)) ? ($r[‘weekviews‘] + 1) : 1;
35 $monthviews = (date(‘Ym‘, $r[‘updatetime‘]) == date(‘Ym‘, SYS_TIME)) ? ($r[‘monthviews‘] + 1) : 1;
36 $db_array = array(‘views‘=>$views,‘yesterdayviews‘=>$yesterdayviews,‘dayviews‘=>$dayviews,‘weekviews‘=>$weekviews,‘monthviews‘=>$monthviews,‘updatetime‘=>SYS_TIME);
37 
38 $db->update(‘content_rank‘,$db_array, array(‘cid‘=>$formdata[‘cid‘],‘id‘=>$formdata[‘id‘]));
39 
40 
41 echo ‘$("#hits").html(‘.$views.‘)‘;
42 ?>

数据库结构:

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 0,
  `views` int(10) unsigned NOT NULL DEFAULT 0,
  `yesterdayviews` int(10) unsigned NOT NULL DEFAULT 0,
  `dayviews` int(10) unsigned NOT NULL DEFAULT 0,
  `weekviews` int(10) unsigned NOT NULL DEFAULT 0,
  `monthviews` int(10) unsigned NOT NULL DEFAULT 0,
  `updatetime` int(10) unsigned NOT NULL DEFAULT 0,
  UNIQUE KEY `cid` (`cid`,`id`),
  KEY `views` (`views`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

加载方式:

内容页面调用js代码

<script type="text/javascript" src="http://www.mamicode.com/{WEBURL}index.php?f=stat.php"></script>

显示文章浏览量

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

 

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

五指cms内容浏览量实现方法