I am trying to create a unique hit counter that records exactly ONE occurrence of each new IP address.
The script first reads the text file line by line, comparing the user's IP to each line read from the file. When it finds a match, it closes the file and terminates. If it reaches the end of the file (doesn't find a match) the read-loop terminates and an fwrite statement occurs.
I receive no errors when the script is ran, however no information is written to the file. I've tried a few variations of code, but nothing seems to work. Below is the most recent attempt at making this work. Any ideas?
$file = "my_hits.txt";
// Obtain users IP address
$ipadd=getenv(REMOTE_ADDR);
// Open $file and search each line (IP address) for a match
$fp = fopen($file,"a+");
while (!feof($fp))
{
$line = fgets($fp, 4096);
$line = trim($line);
if ($line==$ipadd)
{
fclose($fp);
exit;
}
}
// Since the IP was not previously logged, append it to $file and close
fwrite($fp,$ipadd);
fclose($fp);