smonkcaptain Posted August 16, 2010 Share Posted August 16, 2010 Hey everyone! My upcoming site will host, potentiallly thousands of photographs, and i was wondering how to go about having a unique ip hit counter for EACH image. Obviously i dont want people refreshing the page to gain hits, therefore the hit counter will need to be dependent on the IP address. I have used google and found a few hit counters however i need to know how to make this practical as it seems like it will require a lot of databases! Thanks in advanced, Jimmy. Quote Link to comment https://forums.phpfreaks.com/topic/210815-unique-hit-counter/ Share on other sites More sharing options...
MadTechie Posted August 16, 2010 Share Posted August 16, 2010 Just 1 table really CREATE TABLE IF NOT EXISTS `imagecounter` ( `ID` int( NOT NULL auto_increment, `ImageID` int( NOT NULL, `IP` varchar(15) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; I assumed your using an ID as a image reference, but you could update this to use a name instead! <?php mysql_connect("localhost", "USERNAME", "PASSWORD"); mysql_select_db("DATABASE"); $ImageID = $_GET['ImageID']; //Pass your ImageID this could also be a name/referance $query = sprintf("SELECT * FROM `ImageCounter` WHERE `ImageID` =%d AND `IP` = '%s' LIMIT 1", $ImageID, mysql_real_escape_string($_SERVER['REMOTE_ADDR']) ); $result = mysql_query($query); //Check if already added if(mysql_num_rows($result) == 0){ //Add new records $query = sprintf("INSERT INTO `ImageCounter` SET `ImageID` = %d, `IP` = '%s'", $ImageID, mysql_real_escape_string($_SERVER['REMOTE_ADDR']) ); $result = mysql_query($query); } //DISPLAY VIEWS $query = sprintf("SELECT * FROM `ImageCounter` WHERE `ImageID` = %d", $ImageID ); $result = mysql_query($query); echo "This image has been viewed ".mysql_num_rows($result)." times"; ?> as a note i didn't use unique as i assumed the same IP viewing different images would still count up! Quote Link to comment https://forums.phpfreaks.com/topic/210815-unique-hit-counter/#findComment-1099695 Share on other sites More sharing options...
JasonLewis Posted August 16, 2010 Share Posted August 16, 2010 You could store a cookie that lasts for a day, or month, depending on how long a unique hit will last. There's problems with both options, IP addresses can change and cookies can be deleted. It's not that big of a deal though. And you shouldn't require a lot of databases, perhaps a couple tables but that's it, and you could get away with one table. Quote Link to comment https://forums.phpfreaks.com/topic/210815-unique-hit-counter/#findComment-1099696 Share on other sites More sharing options...
MadTechie Posted August 16, 2010 Share Posted August 16, 2010 Oh i forgot to say, these are one 1 visit (ever), you need to add a timestamp and update the first query to suite, the code was just a quick example but I'll add those if needed. Quote Link to comment https://forums.phpfreaks.com/topic/210815-unique-hit-counter/#findComment-1099697 Share on other sites More sharing options...
smonkcaptain Posted August 16, 2010 Author Share Posted August 16, 2010 - MadTechie, Thanks for your code, that is an amazing help. If it's possible to make it so that it's 1 visit per IP, per 24 hours. After the 24 hours, the user with the same IP, will be able to record one more view, intill the next 'refreshed 24 hour cycle'... If that makes any sense? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/210815-unique-hit-counter/#findComment-1099698 Share on other sites More sharing options...
MadTechie Posted August 16, 2010 Share Posted August 16, 2010 Sure Firs update the database with a new field `VisitDate` ALTER TABLE `imagecounter` ADD `VisitDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP then update the code change $query = sprintf("SELECT * FROM `ImageCounter` WHERE `ImageID` =%d AND `IP` = '%s' LIMIT 1", $ImageID, mysql_real_escape_string($_SERVER['REMOTE_ADDR']) ); to $query = sprintf("SELECT * FROM `ImageCounter` WHERE `ImageID` =%d AND `IP` = '%s' AND NOW() <= ADDDATE(`VisitDate`, 1) LIMIT 1", $ImageID, mysql_real_escape_string($_SERVER['REMOTE_ADDR']) ); That should do it, I should say, that the field ID isn't really needed but it depends on how the script intends to grow, so removing it won't cause any short term problems.. Quote Link to comment https://forums.phpfreaks.com/topic/210815-unique-hit-counter/#findComment-1099701 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.