Hey Jen,
Does it equally burden a server to erase duplicated IP?
Yep, it’s basically the same thing. In your specific case, however, you’ll be doing this with logged-in users only so I don’t think your site’s performance will suffer too much.
Edit
Ah, almost forgot. You’ll want to update the function so it doesn’t run for non-logged-in visitors:
/**
* Have WordPress Popular Posts store one view
* per entry per IP per day to keep visitors from
* inflating the views count.
*/
function wpp_unique_view( $post_ID, $views ){
if ( !is_user_logged_in() )
return;
$ip_address = $_SERVER['REMOTE_ADDR'];
$transient_name = 'wpp_' . $post_ID . '_' . $_SERVER['REMOTE_ADDR'];
// Visitor has already seen this page
if ( get_transient( $transient_name ) ) {
exit();
}
// Visitor has not seen this page yet
$timezone = new DateTimeZone( 'GMT' . get_option( 'gmt_offset' ) );
$now = new DateTime( 'now', $timezone );
$midnight = new DateTime( 'tomorrow midnight -1 second', $timezone );
$interval = $midnight->diff( $now );
$expire = $interval->h * 3600 + $interval->i * 60 + $interval->s;
// Keep this record until midnight
set_transient( $transient_name, 1, $expire );
}
add_action( 'wpp_pre_update_views', 'wpp_unique_view', 10, 2 );
-
This reply was modified 6 years, 7 months ago by Hector Cabrera. Reason: Updated code