I have placed a function at the end of my webpage to count the views of the thread, the function is as follows
function add_view($thread_id)
{
global $connection;
$qry = "UPDATE tbl_threads SET views = (views+1) WHERE ";
$qry .= "thread_id= {$thread_id}";
mysqli_query($connection,$qry,MYSQLI_STORE_RESULT);
}
This Function is triggered once at the end of the page. But the problem is When I open the page once, and then see in the database there are 4 views.
Some times it adds 3, some times it adds 4 and some times it adds 2 to the view field of tbl_threads.
It looks that my webpage triggered more than once, Is it Possible, (My webpage has Google Analytics and Google Adsense Codes also)
Can Any one analyse the problem ??
What is the solution of this Problem??
If you use url rewriting, it is possible that the browser requests additional assets (for example favicon.ico
) and if that does not exist and gets rewritten to request your php file, you get another hit. Apple devices are also looking for different icons / images.
This can help you to count only one hit by client.
function add_view($thread_id)
{
global $connection;
if(!isset($_SESSION[$thread_id])) {
$qry = "UPDATE tbl_threads SET views = (views+1) WHERE ";
$qry .= "thread_id= {$thread_id}";
mysqli_query($connection,$qry,MYSQLI_STORE_RESULT);
$_SESSION[$thread_id] = 'viewed';
}
}