Page 1 of 1

dynamic stat counter

Posted: Thu Jan 25, 2007 7:12 am
by beermaker74
I have a real estate listing site an I would like to have a stat counter that the lister can use. My listings page now uses the get method to display the listing. ie
http:mysite.com/listings.php?id=15

so what do you think the best approach to registering the page views?

I have this simple script

Code: Select all

<?php
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
echo $hits[0];
?>
it just uses an include
<

Code: Select all

?php
include ("counter.php");
?>
should I pass the get id to the script and then have the script update the database? Is that too much work im creating for myself? Well any suggestions would be greatly appreciated. Also I am going to use mod rewrite to clean up my urls. If I do that will I have any other issues with the counter?

thanks

Posted: Thu Jan 25, 2007 9:21 am
by feyd
The ID information needs to get to some functionality to update the counter(s).. what actual way you choose is up to you. You could even test several different ways to see what you like most.

Posted: Thu Jan 25, 2007 10:36 am
by pickle
I can see some problems with using a flat file. The main problem being that if two people open the page at the same time, then one of them won't be able to open the counter file, as it will be locked by the other user. I'd use a database to do this - much simpler as well:

Code: Select all

$id = $_GET['id'];
$query = "UPDATE `pageCounterTable` SET `count` = `count` + 1 WHERE `id` = $id";
mysql_query($query);
$query = "SELECT `count` FROM `pageCounterTable` WHERE `id` = $id";
$row = mysql_fetch_assoc(mysql_query($query));
echo $row['count'];
There should be more error checking than what I've got there - but that's the general idea.

When you convert your URLs to look nicer, I would imagine that you'll still be getting the ID of the page somehow. However you get the ID doesn't matter, as long as you can still put it in that query.