Ghostu Posted September 4, 2006 Share Posted September 4, 2006 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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted September 4, 2006 Share Posted September 4, 2006 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. Quote Link to comment Share on other sites More sharing options...
Ghostu Posted September 4, 2006 Author Share Posted September 4, 2006 Thanks very much, worked like a charm ;DHeres 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! Quote Link to comment Share on other sites More sharing options...
Ghostu Posted September 5, 2006 Author Share Posted September 5, 2006 Bump. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.