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

这个防止人不停的刷新页面而产生页面大量浏览了,其实这些对于我们没用作用了,下面我来介绍一个可以防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. function check_cookie($post){
  34. $COOKNAME = 'ashuwp_view';
  35. if(isset($_COOKIE[$COOKNAME]))
  36. $cookie = $_COOKIE[$COOKNAME];
  37. else
  38. return false;
  39. $id = $post->ID;
  40. if(emptyempty($id)){
  41. return false;
  42. }
  43. if(!emptyempty($cookie)){
  44. $list = explode('a', $cookie);
  45. if(!emptyempty($list) && in_array($id, $list)){
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. ### Function: Display The Post Views
  52. function the_views($display = true,$id) {
  53. $post_views = intval(get_post_meta($id,'_check_count',true));
  54. $output = number_format_i18n($post_views);
  55. if($display) {
  56. echo $output;
  57. } else {
  58. return $output;
  59. }
  60. }
  61. ### Function: Display Total Views
  62. if(!function_exists('get_totalviews')) {
  63. function get_totalviews($display = true) {
  64. global $wpdb;
  65. $total_views = intval($wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = '_check_count'"));
  66. if($display) {
  67. echo number_format_i18n($total_views);
  68. } else {
  69. return $total_views;
  70. }
  71. }
  72. }
  73. ### Function: Add Views Custom Fields
  74. add_action('publish_post', 'add_views_fields');
  75. add_action('publish_page', 'add_views_fields');
  76. function add_views_fields($post_ID) {
  77. global $wpdb;
  78. if(!wp_is_post_revision($post_ID)) {
  79. add_post_meta($post_ID, '_check_count', 0, true);
  80. }
  81. }
  82. ### Function: Delete Views Custom Fields
  83. add_action('delete_post', 'delete_views_fields');
  84. function delete_views_fields($post_ID) {
  85. global $wpdb;
  86. if(!wp_is_post_revision($post_ID)) {
  87. delete_post_meta($post_ID, '_check_count');
  88. }
  89. }

第二步,接下来设置Cookie

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

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

这段代码里 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. else{
  7. setPostViews($post_id);
  8. setcookie('views'.$post_id.COOKIEHASH,'1',time() + 3600,COOKIEPATH,COOKIE_DOMAIN);//设置时间间隔
  9. }
  10. ?>