Page Views Counter in PHP is giving the Wrong Results
I am using the following code to count the unique page views of my posts.
$thread_id = 4554; // thread_id is the unique page id
$sess_key = "page_id_" . $thread_id;
if(!isset($_SESSION[$sess_key]))
{
$_SESSION[$sess_key] = 0;
}
if($_SESSION[$sess_key]==0)
{
$query = "UPDATE tbl_threads SET views = (views + 1) WHERE id =
{$thread_id}";
mysqli_query($connection,$query,MYSQLI_STORE_RESULT);
$_SESSION[$sess_key] = 1;
}
But the Problem is, when I add a new post and click it to view it, it
adds, some times 3 views to database and some time 2 views to database.
When I open the post second time, it adds 1 to database. I am about to
pull my hairs, because I did not see any error in this code. It is also
not giving me the unique page views. Please analyse my code and tell me
where is the problem.
Note: I have pasted this code at the end of the webpage before </body> tag.
No comments:
Post a Comment