Jump to content


Photo

Unique hits for CMS


  • Please log in to reply
3 replies to this topic

#1 Ghostu

Ghostu
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 04 September 2006 - 03:11 PM

Hello, Ive been working on a content management system lately.
http://photoshopandyou.com/tutorials/
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:
$sql = "UPDATE tutorials SET hits = hits+1 WHERE id = $id"; 
mysql_query($sql) or die(mysql_error());
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.

#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 04 September 2006 - 03:28 PM

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.

#3 Ghostu

Ghostu
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 04 September 2006 - 05:34 PM

Thanks very much, worked like a charm  ;D
Heres my 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());
}

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!

#4 Ghostu

Ghostu
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 05 September 2006 - 03:07 AM

Bump.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users