解决WordPress文章点击数(浏览量)随页面刷新而增加的问题

网上存在大量关于文章点击数的脚本,大部分都一样,没有解决反刷新和过滤网络爬虫的,不具有真实意义,如果遇到恶意CC攻击,会对服务器造成极大压力特别是

下面提供一种基于cookie的解决

 

 

首先在主题的functions.php 增加如下的脚本:

 

***********文章浏览量防刷新统计开始*********/ 
function statistic_visitors() {  
    if (is_singular()) {
        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,'views',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, ‘views', ($post_views+1))) {  
                 add_post_meta($id, ‘views', 1, true);  
             }  
             }  
         //}  
         }  
      }
    
}
 
add_action(‘wp_head',  ‘statistic_visitors');
 
function check_cookie($post){  
     $COOKNAME = ‘tom_view';  
     if(isset($_COOKIE[$COOKNAME]))  
         $cookie = $_COOKIE[$COOKNAME];  
     else 
         return false;  
     $id = $post->ID;  
     if(empty($id)){  
         return false;  
     }  
     if(!empty($cookie)){  
         $list = explode(‘a', $cookie);  
         if(!empty($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,'views',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 = ‘views'"));  
         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, ‘views', 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, ‘views');  
     }  
}

// 展示点击数

function get_post_views($before = ‘(点击 ‘, $after = ‘ 次)', $echo = 1)
{
  global $post;
  $post_ID = $post->ID;
  $views = (int)get_post_meta($post_ID, ‘views', true);
  if ($echo) echo $before, number_format($views), $after;
  else return $views;
};

/***********文章浏览量防刷新统计结束*********/ 

 

 

$COOKNAME = ‘tom_view'; //cookie名称  
$TIME = 3600;  
$PATH = ‘/';  
 
$id = $posts[0]->ID;  
$expire = time() + $TIME; //cookie有效期  
if(isset($_COOKIE[$COOKNAME]))  
     $cookie = $_COOKIE[$COOKNAME]; //获取cookie  
else 
     $cookie = ”;  
 
if(empty($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);  
     }  

最后在single.php获取其他需要展示点击数的页面,找到类似的代码:

while ( have_posts() ) : the_post(); 后面加入如下代码:

 

get_post_views(‘ ‘, ‘ 次');

 

最后检查(注意有缓存,等一会再刷新)效果:

image

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《解决WordPress文章点击数(浏览量)随页面刷新而增加的问题
本文地址:https://www.zhiletu.com/archives-7180.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微