I have a simple page view tracker that uses a combination of PHP and MySQL to keep a running tally of the number of times the page has been refreshed. There is no complex cookies I just needed to know the raw number refreshes that occur.
It looks like this…
$link = mysql_connect('**********', '*********', '***********');
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("**********", $link);
$query = mysql_query("select value from settings where title like 'search_number'");
$result = mysql_fetch_array($query);
$search_number = $result["value"];
$new_search_number = $search_number + 1;
mysql_query("update settings set value='$new_search_number' where title like 'search_number'");
This code appears to work in all the tests I was capable of running but on the live site it returns ridiculously high numbers. We average 400-500 queries a day according to yahoo, “we use search boss”, but the page tracker reports 8900+ queries a day. Google analytic confirms yahoo’s number. I don’t see how this code could fail due to its simplicity. I was hoping somebody could shed some light on what is going on.
Bots will hit your page more often than you might think. You can compare to your server logs, and I suspect you will see that many requests for your page.
Also, Google Analytics works client-side, requires JavaScript, and does not always run. This has been predicted to account for as many as 10% or so page views. The bulk of the difference though is that Google Analytics filters out a lot of random hits from bots, as hardly any bots even run the analytics code.
On another note, the way you are doing your queries is a bit precarious. I recommend learning how to do prepared queries with PDO, to avoid SQL injection attacks.