PHP hit counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thesimon
Forum Commoner
Posts: 40
Joined: Sun Mar 13, 2005 9:44 pm

PHP hit counter

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

Post by volkan »

andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post 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;); }
marike
Forum Newbie
Posts: 24
Joined: Thu Mar 31, 2005 6:19 pm
Location: New York

Hit Counter

Post 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.
User avatar
Mastermind
Forum Commoner
Posts: 36
Joined: Thu Mar 10, 2005 2:38 am
Location: CDO,Philippines
Contact:

Post by Mastermind »

I think there a big solution try http://www.php.net :arrow:
Post Reply