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
PHP hit counter
Moderator: General Moderators
-
andylyon87
- Forum Contributor
- Posts: 168
- Joined: Sat Jan 31, 2004 5:31 am
- Location: Dundee
you can make one without mysql
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
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("e;<B>Hits:ї$counter]</B>"e;); }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("e;<B>Hits:ї$counter]</B>"e;); }Hit Counter
Using sessions is probably the most common way. You could use something like
the following:
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.
the following:
Code: Select all
<?php
session_start();
$_SESSION['count'] = $_SESSION['count'] + 1;
print "You've looked at this page " . $_SESSION['count'] . ' times.';
?>Shouldn't be too hard to implement.
- Mastermind
- Forum Commoner
- Posts: 36
- Joined: Thu Mar 10, 2005 2:38 am
- Location: CDO,Philippines
- Contact:
I think there a big solution try http://www.php.net 