首页 > CMS > Wordpress > 正文

wordpress可防刷新文章浏览次数统计代码

2024-09-07 00:51:12
字体:大 中 小
来源:转载
供稿:网友

这个防止人不停的刷新页面而产生页面大量浏览了,其实这些对于我们没用作用了,下面我来介绍一个可以防CC或不停刷新而产生没用的浏览次数统计代码.

第一步:按照惯例,把以下代码扔到functions.php里:

  1. /***********文章统计*********/   
  2. function process_postviews() {    
  3.     global $user_ID, $post;    
  4.     if(check_cookie($post))    
  5.         return;    
  6.     if(is_int($post)) {    
  7.         $post = get_post($post);    
  8.     }    
  9.     if(!wp_is_post_revision($post)) {    
  10.         if(is_single() || is_page()) {    
  11.             $id = intval($post->ID);    
  12.             //$post_views = get_post_custom($id);    
  13.             $post_views = get_post_meta($id,'_check_count',true);    
  14.             //统计所有人    
  15.             $should_count = true;    
  16.             //排除机器人    
  17.             $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');    
  18.             $useragent = $_SERVER['HTTP_USER_AGENT'];    
  19.             foreach ($bots as $name => $lookfor) {    
  20.                 if (stristr($useragent, $lookfor) !== false) {    
  21.                     $should_count = false;    
  22.                     break;    
  23.                 }    
  24.             }    
  25.             if($should_count) {    
  26.                 if(!update_post_meta($id, '_check_count', ($post_views+1))) {    
  27.                     add_post_meta($id, '_check_count', 1, true);    
  28.                 }    
  29.             }    
  30.         }    
  31.     }    
  32. }    
  33.  
  34. function check_cookie($post){    
  35.     $COOKNAME = 'ashuwp_view';    
  36.     if(isset($_COOKIE[$COOKNAME]))    
  37.         $cookie = $_COOKIE[$COOKNAME];    
  38.     else   
  39.         return false;    
  40.     $id = $post->ID;    
  41.     if(emptyempty($id)){    
  42.         return false;    
  43.     }    
  44.     if(!emptyempty($cookie)){    
  45.         $list = explode('a', $cookie);    
  46.         if(!emptyempty($list) && in_array($id, $list)){    
  47.             return true;    
  48.         }    
  49.     }    
  50.     return false;    
  51. }    
  52. ### Function: Display The Post Views    
  53. function the_views($display = true,$id) {    
  54.     $post_views = intval(get_post_meta($id,'_check_count',true));    
  55.     $output = number_format_i18n($post_views);    
  56.     if($display) {    
  57.         echo $output;    
  58.     } else {    
  59.         return $output;    
  60.     }    
  61. }    
  62.  
  63. ### Function: Display Total Views    
  64. if(!function_exists('get_totalviews')) {    
  65.     function get_totalviews($display = true) {    
  66.         global $wpdb;    
  67.         $total_views = intval($wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = '_check_count'"));    
  68.         if($display) {    
  69.             echo number_format_i18n($total_views);    
  70.         } else {    
  71.             return $total_views;    
  72.         }    
  73.     }    
  74. }    
  75.  
  76. ### Function: Add Views Custom Fields    
  77. add_action('publish_post', 'add_views_fields');    
  78. add_action('publish_page', 'add_views_fields');    
  79. function add_views_fields($post_ID) {    
  80.     global $wpdb;    
  81.     if(!wp_is_post_revision($post_ID)) {    
  82.         add_post_meta($post_ID, '_check_count', 0, true);    
  83.     }    
  84. }    
  85. ### Function: Delete Views Custom Fields    
  86. add_action('delete_post', 'delete_views_fields');    
  87. function delete_views_fields($post_ID) {    
  88.     global $wpdb;    
  89.     if(!wp_is_post_revision($post_ID)) {    
  90.         delete_post_meta($post_ID, '_check_count');    
  91.     }    
  92. } 

第二步,接下来设置Cookie

在主题的single.php的最最前面加上以下代码:

  1. <?php 
  2. $COOKNAME = 'ashuwp_view'; //cookie名称    
  3. $TIME = 3600 * 24;    
  4. $PATH = '/';    
  5.  
  6. $id = $posts[0]->ID;    
  7. $expire = time() + $TIME; //cookie有效期    
  8. if(isset($_COOKIE[$COOKNAME]))    
  9.     $cookie = $_COOKIE[$COOKNAME]; //获取cookie    
  10. else   
  11.     $cookie = '';    
  12.  
  13. if(emptyempty($cookie)){    
  14.     //如果没有cookie    
  15.     setcookie($COOKNAME, $id, $expire, $PATH);    
  16. }else{    
  17.     //用a分割成数组    
  18.     $list = explode('a', $cookie);    
  19.     //如果已经存在本文的id    
  20.     if(!in_array($id, $list)){    
  21.         setcookie($COOKNAME, $cookie.'a'.$id, $expire, $PATH);    
  22.     }    
  23. }   
  24. ?> 

这段代码里 Cookie的有效期为1天~

第三步,继续修改single.php,查找代码:while( have_posts() ) : the_post();

在它后面加上:process_postviews();

第四步,在你想要显示浏览数的地方加上一下代码:

浏览数:<?php the_views(true,$post->ID);?>

再补充一个

1.首先在主题下functions.php里增加以下代码,这段代码也是网上可以找到的,代码如下:

  1. //add by charleswu 
  2. function getPostViews($postID) { 
  3.     $count_key = 'post_views_count'; 
  4.     $count = get_post_meta($postID, $count_key, true); 
  5.     if ($count == '') { 
  6.         delete_post_meta($postID, $count_key); 
  7.         add_post_meta($postID, $count_key, '0'); 
  8.         return "0"; 
  9.     } 
  10.     return $count; 
  11. } 
  12. function setPostViews($postID) { 
  13.     $count_key = 'post_views_count'; 
  14.     $count = get_post_meta($postID, $count_key, true); 
  15.     if ($count == '') {//www.111cn.net 
  16.         $count = 0; 
  17.         delete_post_meta($postID, $count_key); 
  18.         add_post_meta($postID, $count_key, '0'); 
  19.     } else { 
  20.         $count++; 
  21.         update_post_meta($postID, $count_key, $count); 
  22.     } 
  23. } 

2.解决刷新统计数增加,一定要放在文章页面的最前面,貌似php设置cookie之前不能有输出,我的是single.php页面,代码如下:

  1. <?php 
  2.     $post_id=get_the_ID(); 
  3.     if(isset($_COOKIE['views'.$post_id.COOKIEHASH]) && $_COOKIE['views'.$post_id.COOKIEHASH] == '1') 
  4.     { 
  5.              
  6.     } 
  7.     else{     
  8.         setPostViews($post_id); 
  9.         setcookie('views'.$post_id.COOKIEHASH,'1',time() + 3600,COOKIEPATH,COOKIE_DOMAIN);//设置时间间隔 
  10.     } 
  11. ?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表