Page 1 of 1

PHP hit counter

Posted: Wed Apr 06, 2005 2:19 am
by thesimon
i would like to set up a hit counter in my database with php that allows users of the content management system to track popularity of pages, both statice pages, and pages such as press releases,(newsview.php?newsid=). Does any one have any ideas.

Thanks,
Simon

Posted: Wed Apr 06, 2005 2:28 am
by feyd
not exactly hard to build your own...

A record in the database maps to each page. Every page updates the record's count.

If more detailed "auditing" is needed, then there'd be a seperate record for every query to every page.

Posted: Wed Apr 06, 2005 5:40 am
by volkan

Posted: Fri Apr 08, 2005 4:50 am
by andylyon87
you can make one without mysql

Code: Select all

function hits(){
   //   Hit Counter 
$counter_file = 'hits.txt';
if (file_exists($counter_file)){
$counter = file_get_contents($counter_file);
if (is_numeric($counter)){$counter++;}else{$counter = 0;}
$fp = fopen($counter_file, 'w');
fwrite($fp, $counter);
fclose($fp);}
print(&quote;<B>Hits:&#1111;$counter]</B>&quote;); }
I find that this works better without the else section, if some idiot refreshes loads it resets the counter and as long as you make sure the file has a 0 in it to begin with its simple. Its a hit counter the file will always contain a number.

The better one:

Code: Select all

//   Hit Counter 
$counter_file = 'hits.txt';
if (file_exists($counter_file)){
$counter = file_get_contents($counter_file);
if (is_numeric($counter)){$counter++;}
$fp = fopen($counter_file, 'w');
fwrite($fp, $counter);
fclose($fp);}
print(&quote;<B>Hits:&#1111;$counter]</B>&quote;); }

Hit Counter

Posted: Fri Apr 08, 2005 8:12 pm
by marike
Using sessions is probably the most common way. You could use something like
the following:

Code: Select all

<?php  
session_start();  
$_SESSION['count'] = $_SESSION['count'] + 1;   

print "You've looked at this page " . $_SESSION['count'] . ' times.';
?>
And then use a mySQL insert statement and insert the value of $_SESSION['count'] into your DB table. I'll leave the details to implementing the way to make a system for a pages popularity based on those hits.
Shouldn't be too hard to implement.

Posted: Sat Apr 09, 2005 3:27 am
by Mastermind
I think there a big solution try http://www.php.net :arrow: