Jump to content

Archived

This topic is now archived and is closed to further replies.

Ghostu

Unique hits for CMS

Recommended Posts

Hello, Ive been working on a content management system lately.
[url=http://photoshopandyou.com/tutorials/]http://photoshopandyou.com/tutorials/[/url]
First, a little background on what I have going on here:
In my database I have a table called "tutorials". Each tutorial has its own row in the table.
I have a page called view.php which is the backend of the site, it runs a query and displays a preview of all the tutorials in the database. The preview has a link to the full view of the tutorial, as well as showing the category, date added, and a small description of the tutorial.

What Im trying to do is display the number of unique hits as well. The hit counter you see on the site right now was added by making an extra column in my table called "hits" and using this code:
[code]$sql = "UPDATE tutorials SET hits = hits+1 WHERE id = $id";
mysql_query($sql) or die(mysql_error());[/code]
This works, but its not a unique counter and updates every time someone even refreshes a page, so as you can imagine it doesnt give a very accurate amount of views.
My question is how would I go about turning this into a unique hit counter? Or limit it so a visitor can only add 1 view per hour or something. Ive seen a hit counter like this on other tutorial/article sites, but just havent been able to figure it out. My only idea is to make another column in the table to store the IP addresses that visit each tutorial, but is it even possible to store multiple IP addresses in a single field? So that I can keep track of which addresses visited which tutorial? I made a unique hit counter before using IP addresses, but it made a new row for every IP and thus could only provide a counter for 1 page per table....not good.
Are there any better methods of going about this that Im overlooking?
Thanks for any help, its much appreciated.

Share this post


Link to post
Share on other sites
What you will need to do is create another table in your database, perhaps called unique_hits.

You would need 3 fields: ip, tutorial_id and time.

then, when someone views the page, you will check to see if an entry on the same IP for that tutorial exists. If it does, then dont add to the hit counter, if it doesn't, add to the counter and insert an entry into the new table.

The reason why i say include a time field is because, if i were you, i would remove entries from this new table that are an hour/day old otherwise you are going to have a huge table on your hands.

Share this post


Link to post
Share on other sites
Thanks very much, worked like a charm  ;D
Heres my code:
[code]//Hit counter
//Get visitors IP address
$ip = $_SERVER['REMOTE_ADDR'];
//Check if the IP has been to tutorial before, if not add the IP to the table, also update hits
$fetch = mysql_query("SELECT ip FROM unique_hits WHERE ip = '$ip' AND tutorial_id = '$id'") or die(mysql_error());
if ( mysql_num_rows($fetch) == 0 ) {
  mysql_query("INSERT INTO unique_hits (ip, tutorial_id) VALUES('$ip', '$id')") or die(mysql_error());
  mysql_query("UPDATE tutorials SET hits = hits+1 WHERE id = $id") or die(mysql_error());
}[/code]

I agree about removing old entries from the table, thats a good idea, but I have quick question. I made the field called time as a timestamp, was this the right move and how would I code it to remove IP addresses that are over an hour long?
Thanks again for the help!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.