Hi I want to count page views ..

suppose when user viewing page 1 and spend minimun 10 seconds time on page 1
then i have to calculate view as 1...

so how to do that??

plz give me sample example

Recommended Answers

All 3 Replies

Hi I want to count page views ..

suppose when user viewing page 1 and spend minimun 10 seconds time on page 1
then i have to calculate view as 1...

so how to do that??

plz give me sample example

You will need persistent storage for the number of page views. This can be either a database table, or just a text file etc.

To keep track of page views, each time a page is viewed, increment the page view count in the storage you have. For a database example:

You have a table called "page_views".

Say you have the fields:
id, ip, browser, url, referer, session, enter, leave

Lets say the "enter" field is the time the page was requested, and the "leave" is the time the use left the page.

Then each time the page is requested you update the page_views table with the SQL query:

$ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$browser = mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']);
$referer = mysql_real_escape_string($_SERVER['HTTP_REFERER']);
 $url = mysql_real_escape_string($_SERVER['REQUEST_URI']);
$session = session_id();

$query = "INSERT INTO page_views SET ip = '$ip', browser = '$browser', url = '$url', session = '$session', enter = NOW()";

// do the database insert here

Notice the "enter" is set to "NOW()" which is the current timestamp.

To keep track of time spent on a page, you will need to know when the user left the page.
You can either do this by waiting for the next page request, or you can use client side scripting, such as JavaScript.

The first option does not request JavaScript, but isn't as accurate.

Just wait for the next request, and then match the referer field with the last entries in the database for the same session.

So if the referer is the same as the last url, you know the user left the page at the time of this request.

eg:

$ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$browser = mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']);
$referer = mysql_real_escape_string($_SERVER['HTTP_REFERER']);
 $url = mysql_real_escape_string($_SERVER['REQUEST_URI']);
$session = session_id();

// first update previous page view 
$query = "UPDATE page_views SET leave = NOW() WHERE session = '$session' AND url = '$referer' LIMIT 1";
// do sql query

// insert the new page view
$query = "INSERT INTO page_views SET ip = '$ip', browser = '$browser', url = '$url', session = '$session', enter = NOW()";

// do the database insert here

You'll have to make sure you have the same format for the "url" field and the "referer" field.

HTTP Referers are absolute URLs, so you want to create absolute URLs for the "url" field instead of the simplistic $_SERVER in the example.

Maybe something like:

'http://'.($_SERVER['HTTP_HOST'] ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_ADDR']).$_SERVER['REQUEST_URI'].($_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : '');

Test that out, cause its just a guess.

If you want to use JavaScript, you can set cookies on the client side when ever the page is reloaded. This should always work.

<script>
window.onunload = function() {
setCookie('time_leave', new Date().getTime());
};
</script>

Here setCookie() is a pseudo function.

You can then read this cookie with PHP, and update your leave times in the page_views table.

You will need persistent storage for the number of page views. This can be either a database table, or just a text file etc.

To keep track of page views, each time a page is viewed, increment the page view count in the storage you have. For a database example:

You have a table called "page_views".

Say you have the fields:
id, ip, browser, url, referer, session, enter, leave

Lets say the "enter" field is the time the page was requested, and the "leave" is the time the use left the page.

Then each time the page is requested you update the page_views table with the SQL query:

$ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$browser = mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']);
$referer = mysql_real_escape_string($_SERVER['HTTP_REFERER']);
 $url = mysql_real_escape_string($_SERVER['REQUEST_URI']);
$session = session_id();

$query = "INSERT INTO page_views SET ip = '$ip', browser = '$browser', url = '$url', session = '$session', enter = NOW()";

// do the database insert here

Notice the "enter" is set to "NOW()" which is the current timestamp.

To keep track of time spent on a page, you will need to know when the user left the page.
You can either do this by waiting for the next page request, or you can use client side scripting, such as JavaScript.

The first option does not request JavaScript, but isn't as accurate.

Just wait for the next request, and then match the referer field with the last entries in the database for the same session.

So if the referer is the same as the last url, you know the user left the page at the time of this request.

eg:

$ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$browser = mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']);
$referer = mysql_real_escape_string($_SERVER['HTTP_REFERER']);
 $url = mysql_real_escape_string($_SERVER['REQUEST_URI']);
$session = session_id();

// first update previous page view 
$query = "UPDATE page_views SET leave = NOW() WHERE session = '$session' AND url = '$referer' LIMIT 1";
// do sql query

// insert the new page view
$query = "INSERT INTO page_views SET ip = '$ip', browser = '$browser', url = '$url', session = '$session', enter = NOW()";

// do the database insert here

You'll have to make sure you have the same format for the "url" field and the "referer" field.

HTTP Referers are absolute URLs, so you want to create absolute URLs for the "url" field instead of the simplistic $_SERVER in the example.

Maybe something like:

'http://'.($_SERVER['HTTP_HOST'] ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_ADDR']).$_SERVER['REQUEST_URI'].($_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : '');

Test that out, cause its just a guess.

If you want to use JavaScript, you can set cookies on the client side when ever the page is reloaded. This should always work.

<script>
window.onunload = function() {
setCookie('time_leave', new Date().getTime());
};
</script>

Here setCookie() is a pseudo function.

You can then read this cookie with PHP, and update your leave times in the page_views table.

BTW: to get the count of page views, you'd query the db table.

eg:

select * from page_views where UNIX_TIMESTAMP(leave) - UNIX_TIMESTAMP(enter) > 60;

Probably not the best query, but you get the idea.

Thanks digital-ether,
for your exaplanation with example.
i got the idea

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.