Forum Moderators: coopster

Message Too Old, No Replies

How to track the number of times a page has been viewed?

         

dbarasuk

11:38 pm on Dec 15, 2007 (gmt 0)

10+ Year Member



Hello,

It's beyond my knowledge of PHP programming to write a script that would keep track of how many times a particular page has been viewed.

Any suggested script please or reference of how to learn this?

Best regards,
dbarasuk

jatar_k

12:06 am on Dec 16, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could have a database and a row for each page, or just one page if that is what you want to track.

if you just want a hit counter type action then you would just increment a number which would represent 'views' the trouble with this is that it would increment every time you refresh the page.

if you wanted a more unique view then when someone hits the page you could store information about that user, user agent, ip, whatever you like.

you could also do it to file if you were only tracking a single page

dbarasuk

12:55 pm on Dec 16, 2007 (gmt 0)

10+ Year Member



Thanks,
but can you just give me a working code to track only one page, please? or just point me to a tutorial doing that.

Sorry, I want to see (an image worth 1000 words)

Back to you jatar_k

jatar_k

3:44 pm on Dec 16, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



here's the counter style

create a file called tracker.txt with just a 1 in it, no returns, nothing else, put this in the same dir as this file

<?php
$mytrack = 'tracker.txt';
$oc = file_get_contents($mytrack);
$nc = $oc + 1;
$fp = fopen($mytrack,'w');
fwrite($fp,$nc);
fclose($fp);
echo 'old: ',$oc,'<br>new: ',$nc;
?>

it still has test code in there, you can get rid of that echo line once you see how it works

that's it