Hello, I am working on a counter which will record unique page views so that users can see how many times an advert has been visited like on ebay.
I have chosen to go the ip address route, as this seems to be more reliable than a cookie (would a cookie be better). However this means that I have to store every ip address that goes to every advert (up to 10k ads on the website) which could potentially come to a lot of data.
I found some code at http://www.trap17.com/index.php/php-unique-hit-counter_t8117.html which I intend to modify for the purpose:
<?php
//The file where IPs are logged.
$filename = "hits.txt";
//Writing the IPs to the file hits.txt
$fd = fopen ($filename , "r");
$fstring = fread ($fd , filesize ($filename));
fclose($fd);
$fd = fopen ($filename , "w");
$fcounted = $fstring."\n".getenv("REMOTE_ADDR");
$fout= fwrite ($fd , $fcounted );
fclose($fd);
//Counting the hits from hits.txt.
$file = file($filename);
$file = array_unique($file);
$hits = count($file);
echo $hits;
?>
I am going to convert this so that the data is saved in a database instead of a txt file. It doesn't seem very efficient to me that the number of unique ips is counted every time an advert is viewed. I guess it wouldn't take up much cpu power, but over 10k ads on a popular website could this become an issue?
Is this method the best way to do this, or is there a quicker, more efficient way that I have overlooked?
Thanks in advance.