To keep this simple, let's say I'm making a basic pageview counter in php that stores the counts for each page in a mysql table. The table had 2 colums: PAGE_ID and COUNT.
I added the following code to every page:
$query = "INSERT INTO table VALUES ('$page_id', '1')
ON duplicate KEY UPDATE COUNT=COUNT+1";
$result = mysqli_query($cxn, $query);
To make sure each person viewing the page only triggers the counter once I added PHP sessions. Basically if you view a page, the page_id gets stored in a session and the counter php code checks that session before triggering the counter. Worked fine in my own testing.
Some pages were getting too many views, I was suspecting duplicates, so I started logging IPs and User agents. It turns out in about 10% of the cases, the IP triggers the counter for the same page 2-3 times in a few minutes.
First question What could be causing the duplicates? The problem seems to happen mostly with IE8 and Safari but I also have at least one instance of it happening with IE7 and IE6. Any known problem with php sessions? Should I use cookies instead?
Part 2: I modified my table so that it now stores the last unix time stamp and the last IP that triggered the counter.
I want to modify my query so that before it runs the "COUNT=COUNT+1" it checks for the following:
If the current IP is the same as the last stored IP for this page {
check that it's been at least 5 minutes before doing COUNT=COUNT+1
} else { COUNT=COUNT+1; }
Second question How do I write that in a mysql query, while keeping my "ON duplicate KEY" statement?
I understand this statement wouldn't be 100% accurate but until I can figure out why the session thing doesn't seem to be working, this would work. My website is low traffic and I rarely get more than 1 visitors on the same page within a 5 minutes time frame.
Thanks