3

I've searched online for a way to disable post/page view "hacking" with refreshing page but I didn't find any.

How to not increase page/post view count with refresh?

or same question from different angle:

How to register only unique visits?

My current thoughts are IP or nonce but Im afraid I need a little help to get started.


Current code is super simple:

    /* Allow devs to override the meta key used. By default, this is 'Views'. */
    $meta_key = 'estate_property_views_count';

    /* Get the number of views the post currently has. */
    $old_views = get_post_meta( $post_id, $meta_key, true );

    /* Add +1 to the number of current views. */
    $new_views = absint( $old_views ) + 1;

    /* Update the view count with the new view count. */
    update_post_meta( $post_id, $meta_key, $new_views, $old_views );
Solo
  • 6,687
  • 7
  • 35
  • 67
  • Possibly duplicate http://stackoverflow.com/questions/4001973/tracking-unique-visitors-only – AnkiiG Nov 20 '15 at 06:12

1 Answers1

3

Method 1 - Using Cookies:

Instead of IP you can do it using cookies. You need to first check if cookie exists, if it does not then update and set cookie otherwise don't.

Something like this will work:

if(!isset($_COOKIE['not_unique'])) {
    setcookie('not_unique', '1', time() + (86400 * 30)); //30 days
    $old_views = get_post_meta( $post_id, $meta_key, true );
    $new_views = absint( $old_views ) + 1;
    update_post_meta( $post_id, $meta_key, $new_views, $old_views );
}

Method 2 - Using Sessions:

Add session_start(); at the very first line of your script and then use it like this:

if(!isset($_SESSION['not_unique'])) {
    $_SESSION['not_unique'] = 1;
    $old_views = get_post_meta( $post_id, $meta_key, true );
    $new_views = absint( $old_views ) + 1;
    update_post_meta( $post_id, $meta_key, $new_views, $old_views );
}
Muhammad Bilal
  • 2,106
  • 1
  • 15
  • 24
  • It doesn't work for sure if cookies are disabled (well, I don't think there are many people who has cookies disabled, that's not a big deal) **but** how about "incognito" modes of browsers? Also not working, right? – Solo Nov 20 '15 at 06:16
  • @Solo yes you are right, this is the easiest solution, if you want a more robust solution what you can do is save each users ip in database and than before updating views count check if the ip exists in db or not – Muhammad Bilal Nov 20 '15 at 06:18
  • Thank you for your answers! Are you also familiar with sessions? Could it be used somehow? I would love to have "temporary" solution - for example count view every 5 minutes, database is overkill for my needs. – Solo Nov 20 '15 at 06:22
  • Sessions destroyed when user close brrowser – Muhammad Bilal Nov 20 '15 at 06:22
  • @madforstrength Counting users with IP isn't a good idea as there may be multiple users sitting behind a single IP. Cookies and sessions are what you may play around to achieve desired results. – Rehmat Nov 20 '15 at 06:24
  • @rehmat Every method has its own pros and cons, we can't say any one approach is perfect. It depends on the requirements. – Muhammad Bilal Nov 20 '15 at 06:27
  • Thanks guys! I think sessions are better for my needs - closing browser seems a bit bigger headache for "hacker" or normal user than just disabling cookies or use incognito mode. I've never used sessions & need to learn them first. If you're familiar with sessions, I would love to see a basic example in your answer, if not, don't worry about it. – Solo Nov 20 '15 at 06:28