wordpress可防刷新文章浏览次数统计代码
这个防止人不停的刷新页面而产生页面大量浏览了,其实这些对于我们没用作用了,下面我来介绍一个可以防CC或不停刷新而产生没用的浏览次数统计代码.
第一步:按照惯例,把以下代码扔到functions.php里:
-
- 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_meta($id,'_check_count',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, '_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');
- }
- }
第二步,接下来设置Cookie
在主题的single.php的最最前面加上以下代码:
- <?php
- $COOKNAME = 'ashuwp_view';
- $TIME = 3600 * 24;
- $PATH = '/';
-
- $id = $posts[0]->ID;
- $expire = time() + $TIME;
- if(isset($_COOKIE[$COOKNAME]))
- $cookie = $_COOKIE[$COOKNAME];
- else
- $cookie = '';
-
- if(emptyempty($cookie)){
-
- setcookie($COOKNAME, $id, $expire, $PATH);
- }else{
-
- $list = explode('a', $cookie);
-
- if(!in_array($id, $list)){
- setcookie($COOKNAME, $cookie.'a'.$id, $expire, $PATH);
- }
- }
- ?>
这段代码里 Cookie的有效期为1天~
第三步,继续修改single.php,查找代码:while( have_posts() ) : the_post();
在它后面加上:process_postviews();
第四步,在你想要显示浏览数的地方加上一下代码:
浏览数:<?php the_views(true,$post->ID);?>
再补充一个
1.首先在主题下functions.php里增加以下代码,这段代码也是网上可以找到的,代码如下:
-
- 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 == '') {
- $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.解决刷新统计数增加,一定要放在文章页面的最前面,貌似php设置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);
- }
- ?>
|