Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

Page view counter using PHP

Links for php scripts

Moderators: macek, egami, gesf

Page view counter using PHP

Postby phpkiller » Wed Apr 16, 2014 3:42 am

The logic is to create a file and read it. Now put a incremented value in it.
Next time when you need the counter value just read it again and get the counter value.

define("HITCOUNT", "hitcounter.txt");

function addOne() {
$fh = fopen(HITCOUNT, "r");
$count = fread($fh, filesize(HITCOUNT));
fclose($fh); $count = $count +1;
$fh = fopen(HITCOUNT, "w");
fwrite($fh, $count);

/* count Variables */
function getcount() {
$fh = fopen(HITCOUNT, "r");
$count = fread($fh, filesize(HITCOUNT));
return $count;
//call function to add one
echo "This page has been accessed ";
echo getCount();
New php-forum User
New php-forum User
Posts: 31
Joined: Thu Jun 28, 2012 2:11 pm

Re: Page view counter using PHP

Postby seandisanti » Tue Apr 22, 2014 12:33 am

There are a lot of ways that could go wrong, multiple concurrent accesses for example. A database record would be a better option. you can just insert a record in a database each time the file is accessed and have it autoincrement and have a timestamp field. Then you can query at any time to see up to the second stats. Even better would be to just use google analytics and let them track and cut the data up for you. all you have to do is put a script in your header or footer and then read your reports or make use of their api to view or display your metrics including what links were clicked from what pages. Plus every other bit of info from the headers including browser, OS if detected, screen size, etc etc etc.
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.