Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/210815-unique-hit-counter/
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/210815-unique-hit-counter/#findComment-1099695
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/210815-unique-hit-counter/#findComment-1099696
Share on other sites

- 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!

Link to comment
https://forums.phpfreaks.com/topic/210815-unique-hit-counter/#findComment-1099698
Share on other sites

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..

 

Link to comment
https://forums.phpfreaks.com/topic/210815-unique-hit-counter/#findComment-1099701
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.