国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 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.     }    

第二步,接下來設置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. function setPostViews($postID) { 
  12.     $count_key = 'post_views_count'
  13.     $count = get_post_meta($postID$count_key, true); 
  14.     if ($count == '') {//www.111cn.net 
  15.         $count = 0; 
  16.         delete_post_meta($postID$count_key); 
  17.         add_post_meta($postID$count_key'0'); 
  18.     } else { 
  19.         $count++; 
  20.         update_post_meta($postID$count_key$count); 
  21.     } 

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. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿拉尔市| 公安县| 和静县| 合阳县| 邻水| 张家界市| 五华县| 遂溪县| 潼南县| 博兴县| 兴隆县| 申扎县| 江西省| 新乡县| 南宫市| 平昌县| 奉化市| 重庆市| 新河县| 红桥区| 奉贤区| 宁波市| 原平市| 衡水市| 洪湖市| 尉氏县| 香格里拉县| 祁门县| 柘荣县| 容城县| 华安县| 湖北省| 萍乡市| 横山县| 太湖县| 淮滨县| 双牌县| 郑州市| 双辽市| 杂多县| 留坝县|