Jump to content

Counting Page Views


thara

Recommended Posts

Hi all,

 

Im looking for some help with a page views counter in php.

 

suppose when user viewing a profile page and spend minimun 10 seconds time on that page

then i have to calculate view as 1...

so how to do that??

 

I have stored all profile details in a database and all pulled through the same template page, like profile.php?id=12345.

 

can anybody give me sample example

 

Thank you..

Link to comment
Share on other sites

You'll need to use Javascript and AJAX for this. SetTimeout () should be a good start for the JS end.

 

However, I don't think it's a good idea, as how long a page is open doesn't say anything about whether or not it has actually been read (which I assume is the point of this idea). For example, I routinely have lots of tabs open in my browser. Some stuff is stuff I'm going to read later, some of it for reference, and some just because I'm too lazy to close them (:P). Also, there's been times where I've read the content, and closed the page, before spending 10 seconds on the site. Not to mention those who don't have JS (activated).

That's why I recommend that you increase the counter when you're actually displaying the page. Lot less complexity needed, and will give a more accurate picture of the situation. You'll still don't know how many times the page has actually been read, but that you will never know anyway.

Link to comment
Share on other sites

  • 2 weeks later...

I tried with this script to counts how many users have viewed in my website's pages. This is my code

 

<?php
require_once ('test.php');

$institute_id = 14;

$q = "INSERT INTO page_views2 ( institute_id, views) VALUES ( $institute_id, 1)
  ON DUPLICATE KEY UPDATE views=views+1"
  ;
$r = mysqli_query ($dbc, $q);

?>

 

I added this to top of my webpages and this is working (views incrementing) properly when I open the pages. But my problem is When I refresh the page, page views is incrementing by 1. It is okey when it is open first time. But I want to avoid from this when someone is refreshing the page.

so can any body tell me how can I do this?

 

any comments are greatly appreciated.

 

thank you.

Link to comment
Share on other sites

if the user refresh tha page that should encremenr because that is considered as views. but if you dont like it then on your table where you have the counter add another field for the current page so if the current page save there is index.php it should not increment since thats also the last page visited.

 

or store on session the last page visited

Link to comment
Share on other sites

thanks for reply...

 

I tried it with cookies... and this is my code...

 

<?php
require_once ('test.php');

$institute_id = 14;
//if no such cookie exists, assume that its their first time viewing.
if(!isset($_COOKIE['institute_id'])){

 $q = "INSERT INTO page_views2 ( institute_id, views) VALUES ( $institute_id, 1)
 ON DUPLICATE KEY UPDATE views=views+1";
 $r = mysqli_query ($dbc, $q);

 //set cookie saying they've viewed this institutions page.
 setcookie( 'institute_id', $institute_id, time()+1800);
}

?>

 

this is working.. when a user open a page and views increment by 1. then It is valid for 30 minutes from the time page opened. Can anybody tell me this is a good solution for me or is there any good solution for this than my solution?

 

any comments are greatly appreciated.

 

Thank you.

Link to comment
Share on other sites

I used this script to count page views in my project. It is working. But when I open several pages on several tabs in same browser its incrementing only one page and others not incrementing. Can anybody tell what is the reason?

 

This is my script..

 

<?php
require_once ('test.php');
$institute_id = 14;
//if no such cookie exists, assume that its their first time viewing.
if(!isset($_COOKIE['institute_id'])){
 $q = "INSERT INTO page_views2 ( institute_id, views) VALUES ( $institute_id, 1)
	 ON DUPLICATE KEY UPDATE views=views+1";
 $r = mysqli_query ($dbc, $q);

 //set cookie saying they've viewed this institutions page.
 setcookie( 'institute_id', $institute_id, time()+1800);
}

?>

 

 

Thanks you..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.