dynamic stat 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
beermaker74
Forum Newbie
Posts: 6
Joined: Fri Sep 22, 2006 11:55 am

dynamic stat counter

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

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply