wordpress可防刷新文章浏览次数统计代码
发布:smiling 来源: PHP粉丝网 添加日期:2014-07-25 11:20:51 浏览: 评论:0
这个防止人不停的刷新页面而产生页面大量浏览了,其实这些对于我们没用作用了,下面我来介绍一个可以防CC或不停刷新而产生没用的浏览次数统计代码.
第一步:按照惯例,把以下代码扔到functions.php里:
- /***********文章统计*********/
- function process_postviews() {
- global $user_ID, $post;
- if(check_cookie($post))
- return;
- if(is_int($post)) {
- $post = get_post($post);
- }
- if(!wp_is_post_revision($post)) {
- if(is_single() || is_page()) {
- $id = intval($post->ID);
- //$post_views = get_post_custom($id);
- $post_views = get_post_meta($id,'_check_count',true);
- //统计所有人
- $should_count = true;
- //排除机器人
- $bots = array('Google Bot' => 'googlebot', 'Google Bot' => 'google', 'MSN' => 'msnbot', 'Alex' => 'ia_archiver', 'Lycos' => 'lycos', 'Ask Jeeves' => 'jeeves', 'Altavista' => 'scooter', 'AllTheWeb' => 'fast-webcrawler', 'Inktomi' => 'slurp@inktomi', 'Turnitin.com' => 'turnitinbot', 'Technorati' => 'technorati', 'Yahoo' => 'yahoo', 'Findexa' => 'findexa', 'NextLinks' => 'findlinks', 'Gais' => 'gaisbo', 'WiseNut' => 'zyborg', 'WhoisSource' => 'surveybot', 'Bloglines' => 'bloglines', 'BlogSearch' => 'blogsearch', 'PubSub' => 'pubsub', 'Syndic8' => 'syndic8', 'RadioUserland' => 'userland', 'Gigabot' => 'gigabot', 'Become.com' => 'become.com','Baidu Bot'=>'Baiduspider');
- $useragent = $_SERVER['HTTP_USER_AGENT'];
- foreach ($bots as $name => $lookfor) {
- if (stristr($useragent, $lookfor) !== false) {
- $should_count = false;
- break;
- }
- }
- if($should_count) {
- if(!update_post_meta($id, '_check_count', ($post_views+1))) {
- add_post_meta($id, '_check_count', 1, true);
- }
- }
- }
- }
- }
- function check_cookie($post){
- $COOKNAME = 'ashuwp_view';
- if(isset($_COOKIE[$COOKNAME]))
- $cookie = $_COOKIE[$COOKNAME];
- else
- return false;
- $id = $post->ID;
- if(emptyempty($id)){
- return false;
- }
- if(!emptyempty($cookie)){
- $list = explode('a', $cookie);
- if(!emptyempty($list) && in_array($id, $list)){
- return true;
- }
- }
- return false;
- }
- ### Function: Display The Post Views
- function the_views($display = true,$id) {
- $post_views = intval(get_post_meta($id,'_check_count',true));
- $output = number_format_i18n($post_views);
- if($display) {
- echo $output;
- } else {
- return $output;
- }
- }
- ### Function: Display Total Views
- if(!function_exists('get_totalviews')) {
- function get_totalviews($display = true) {
- global $wpdb;
- $total_views = intval($wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = '_check_count'"));
- if($display) {
- echo number_format_i18n($total_views);
- } else {
- return $total_views;
- }
- }
- }
- ### Function: Add Views Custom Fields
- add_action('publish_post', 'add_views_fields');
- add_action('publish_page', 'add_views_fields');
- function add_views_fields($post_ID) {
- global $wpdb;
- if(!wp_is_post_revision($post_ID)) {
- add_post_meta($post_ID, '_check_count', 0, true);
- }
- }
- ### Function: Delete Views Custom Fields
- add_action('delete_post', 'delete_views_fields');
- function delete_views_fields($post_ID) {
- global $wpdb;
- if(!wp_is_post_revision($post_ID)) {
- delete_post_meta($post_ID, '_check_count');
- }
- }
第二步,接下来设置Cookie
在主题的single.php的最最前面加上以下代码:
- <?php
- $COOKNAME = 'ashuwp_view'; //cookie名称
- $TIME = 3600 * 24;
- $PATH = '/';
- $id = $posts[0]->ID;
- $expire = time() + $TIME; //cookie有效期
- if(isset($_COOKIE[$COOKNAME]))
- $cookie = $_COOKIE[$COOKNAME]; //获取cookie
- else
- $cookie = '';
- if(emptyempty($cookie)){
- //如果没有cookie
- setcookie($COOKNAME, $id, $expire, $PATH);
- }else{
- //用a分割成数组
- $list = explode('a', $cookie);
- //如果已经存在本文的id
- if(!in_array($id, $list)){
- setcookie($COOKNAME, $cookie.'a'.$id, $expire, $PATH);
- }
- }
- ?>
这段代码里 Cookie的有效期为1天~
第三步,继续修改single.php,查找代码:while( have_posts() ) : the_post();
在它后面加上:process_postviews();
第四步,在你想要显示浏览数的地方加上一下代码:
浏览数:<?php the_views(true,$post->ID);?>
再补充一个
1.首先在主题下functions.php里增加以下代码,这段代码也是网上可以找到的,代码如下:
- //add by charleswu
- function getPostViews($postID) {
- $count_key = 'post_views_count';
- $count = get_post_meta($postID, $count_key, true);
- if ($count == '') {
- delete_post_meta($postID, $count_key);
- add_post_meta($postID, $count_key, '0');
- return "0";
- }
- return $count;
- }
- function setPostViews($postID) {
- $count_key = 'post_views_count';
- $count = get_post_meta($postID, $count_key, true);
- if ($count == '') {//www.111cn.net
- $count = 0;
- delete_post_meta($postID, $count_key);
- add_post_meta($postID, $count_key, '0');
- } else {
- $count++;
- update_post_meta($postID, $count_key, $count);
- }
- }
2.解决刷新统计数增加,一定要放在文章页面的最前面,貌似php设置cookie之前不能有输出,我的是single.php页面,代码如下:
- <?php
- $post_id=get_the_ID();
- if(isset($_COOKIE['views'.$post_id.COOKIEHASH]) && $_COOKIE['views'.$post_id.COOKIEHASH] == '1')
- {
- }
- else{
- setPostViews($post_id);
- setcookie('views'.$post_id.COOKIEHASH,'1',time() + 3600,COOKIEPATH,COOKIE_DOMAIN);//设置时间间隔
- }
- ?>
Tags: wordpress 防刷新文章 浏览次数
- 上一篇:WordPress禁用目录浏览配置方法
- 下一篇:wordpress垃圾评论的方法
相关文章
- ·WordPress初级教程1:什么是博客?(2013-11-11)
- ·WordPress初级教程-2: 什么是WordPress?(2013-11-11)
- ·WordPress初级教程-3: WordPress的功能和特点(2013-11-11)
- ·WordPress初级教程-4: 选择WordPress博客的主机和域名(2013-11-11)
- ·WordPress初级教程-5: 安装WordPress(2013-11-11)
- ·WordPress初级教程-6: 本地安装WordPress(2013-11-11)
- ·WordPress初级教程-7: 一个数据库中安装多个WordPress博客(2013-11-11)
- ·WordPress初级教程-8: WordPress控制面板/ Dashboard(2013-11-11)
- ·WordPress初级教程-9: WordPress用户设置/ Users(2013-11-11)
- ·WordPress初级教程-10: WordPress博客配置/ Settings(2013-11-11)
- ·关于wordpress上传图片不显示的原因(2013-11-11)
- ·WordPress程序的脆弱点你知道吗 (2013-11-11)
- ·总结八大Wordpress网站百度收录实现秒收的方法绝招 (2013-11-11)
- ·WordPress如何网站投稿者也可以上传图片(2014-03-18)
- ·WordPress怎么修改新用户注册邮件内容(2014-03-18)
- ·WordPress怎么添加前台注册功能(2014-03-18)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)