Posts views count by IP

I have this code for count the views of page :

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;
}
}

the problem is when i visit the page every time add one view , how to ignore the same IP from added again ?

Related posts

Leave a Reply

2 comments

  1. Save the user’s IP address into an array. Save the JSON encoded version of the array into the database then decode and loop through it to match the IP addresses.

    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
    
    }
    

    Hope that helps!

  2. Some little improvement to the script above.

    Add IP anonymously with wp_hash() and a count based on array count.

    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
    
       } 
    }