帖子浏览量按IP计数


Posts views count by IP

我有这个代码来计算页面的浏览量:

remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
function PostViews($postID) {
$key = 'post_views_count';
$count = get_post_meta($postID, $key, true);
if($count==''){
    $count = 1;
    delete_post_meta($postID, $key);
    add_post_meta($postID, $key, '1');
     return $count;
}else{
    $count++;
    update_post_meta($postID, $key, $count);
    return $count;
}
}

问题是,当我每次访问该页面时添加一个视图,如何忽略再次添加的相同IP?

将用户的IP地址保存到一个数组中。将数组的JSON编码版本保存到数据库中,然后对其进行解码并循环以匹配IP地址。

function update_post_views( $post_id ) {
    // The user's IP address
    $user_ip = $_SERVER['REMOTE_ADDR'];
    $views_key = 'post_views_count'; // The views post meta key
    $ip_key = 'post_views_ip'; // The IP Address post meta key
    // The current post views count
    $count = get_post_meta( $post_id, $views_key, true ); 
    // Array of IP addresses that have already visited the post.
    if ( get_post_meta( $post_id, $ip_key, true ) != '' ) {
        $ip = json_decode( get_post_meta( $post_id, $ip_key, true ), true );
    } else {
        $ip = array(); 
    }
    /*
        The following checks if the user's IP already exists
    */
    for ( $i = 0; $i < count( $ip ); $i++ ) {
        if ( $ip[$i] == $user_ip )
            return false;
    }
    /* 
        If the script has gotten this far, it means that 
        the user's IP address isn't in the database.
    */
    // Update and encode the $ip array into a JSON string
    $ip[ count( $ip ) ] = $user_ip;
    $json_ip = json_encode( $ip );
    // Update the post's metadata 
    update_post_meta( $post_id, $views_key, $count++ ); // Update the count
    update_post_meta( $post_id, $ip_key, $json_ip ); // Update the user IP JSON obect
}

希望能有所帮助!

对上面的脚本做了一些小改进。

使用wp_hash()和基于数组计数的计数匿名添加IP。

if (!function_exists('update_post_views')) {
function update_post_views($post_id) {
    // return if $_SERVER['REMOTE_ADDR'] not exist
    if (!isset($_SERVER['REMOTE_ADDR'])) {
        return;
    }
    // The user's IP address
    $user_ip = $_SERVER['REMOTE_ADDR'];

    // hash current ip to make it anonymous
    $user_ip = wp_hash($user_ip);

    $views_key = 'post_views_count'; // The views post meta key
    $ip_key = 'post_views_ip'; // The IP Address post meta key

    // Array of IP addresses that have already visited the post.
    if (get_post_meta($post_id, $ip_key, true) != '') {
        $ip = json_decode(get_post_meta($post_id, $ip_key, true), true);
    } else {
        $ip = array();
    }
    /*
    The following checks if the user's IP already exists
     */
    for ($i = 0; $i < count($ip); $i++) {
        if ($ip[$i] == $user_ip) {
            return false;
        }
    }
    /* 
    If the script has gotten this far, it means that 
    the user's IP address isn't in the database.
    */
    // Update and encode the $ip array into a JSON string
    $ip[count($ip)] = $user_ip;
    $json_ip = json_encode($ip);

    // Update the post's metadata 
    update_post_meta($post_id, $views_key, (int)count($ip)); // Update the count
    update_post_meta($post_id, $ip_key, $json_ip); // Update the user IP JSON obect
   } 
}