這個防止人不停的刷新頁面而產(chǎn)生頁面大量瀏覽了,其實這些對于我們沒用作用了,下面我來介紹一個可以防CC或不停刷新而產(chǎn)生沒用的瀏覽次數(shù)統(tǒng)計代碼.
第一步:按照慣例,把以下代碼扔到functions.php里:
- /***********文章統(tǒng)計*********/
- 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);
- //統(tǒng)計所有人
- $should_count = true;
- //排除機(jī)器人
- $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');
- }
- }
第二步,接下來設(shè)置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分割成數(shù)組
- $list = explode('a', $cookie);
- //如果已經(jīng)存在本文的id
- if(!in_array($id, $list)){
- setcookie($COOKNAME, $cookie.'a'.$id, $expire, $PATH);
- }
- }
- ?>
這段代碼里 Cookie的有效期為1天~
第三步,繼續(xù)修改single.php,查找代碼:while( have_posts() ) : the_post();
在它后面加上:process_postviews();
第四步,在你想要顯示瀏覽數(shù)的地方加上一下代碼:
瀏覽數(shù):<?php the_views(true,$post->ID);?>
再補充一個
1.首先在主題下functions.php里增加以下代碼,這段代碼也是網(wǎng)上可以找到的,代碼如下:
- //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.解決刷新統(tǒng)計數(shù)增加,一定要放在文章頁面的最前面,貌似php設(shè)置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);//設(shè)置時間間隔
- }
- ?>
新聞熱點
疑難解答
圖片精選