0

I have a view counter on my blog which tracks how many times certain article has been viewed, however, this plugin that I use allows for endless hits simply by pressing F5.

I'm trying to edit the plugin so that it can only count one view per page visit.

I already created a cookie to prevent this from happening, which seems to work fine. Of course cookies could easily be disabled. This is why I tried to achieve the same thing using a PHP session.

Here is my code that, for some reason, doesn't seem to be working (the cookie works perfectly though):

session_start();
    if(($_COOKIE['last_ip_address_' . $id]!= $_SERVER['REMOTE_ADDR'] . '_' . $id) 
    || ($_SESSION['last_ip_address_' . $id]!= $_SERVER['REMOTE_ADDR'] . '_' . $id)) {
        if(!update_post_meta($id, 'views', ($post_views+1))) 
            add_post_meta($id, 'views', 1, true);
        }
        $_SESSION['last_ip_address_' . $id] = $_SERVER['REMOTE_ADDR'] . '_' . $id;  
        setcookie('last_ip_address_' . $id, $_SERVER['REMOTE_ADDR'] . '_' . $id, time()+3600);
        }
    }
Swen
  • 767
  • 1
  • 9
  • 31
  • 1
    PHP Sessions either require cookies or a GET parameter to be sent, both are easy to work around to artificially inflate views, have you throught of just logging IPs to a database? - http://stackoverflow.com/a/1376744/1400370 – Jamie Taylor Jan 28 '14 at 15:33
  • 1
    I was going to say the same thing. Your session will eventually expire, so though users might not be able to spam F5, the same user will still count for multiple views. – NobleUplift Jan 28 '14 at 15:38
  • 1
    "Most notably, a single user can potentially use a different IP address for each request (as is the case with large ISPs such as AOL), and multiple users can potentially use the same IP address (as is the case in many computer labs using an HTTP proxy).", http://shiflett.org/articles/the-truth-about-sessions. Symfony2 has an evaluated session system, but that is out of question. Why not use HTML5 Web Storage? http://www.w3schools.com/html/html5_webstorage.asp. Still changing the browser would count for a new visitor. ;/ – loveNoHate Jan 28 '14 at 15:41
  • I will use the cookie + IP logging method for this. Seems that I misunderstood how sessions work. Thank you for the replies! – Swen Jan 28 '14 at 16:03

0 Answers0