This should work:
// set the file and open mode
$file = "counter.txt";
$mode = "r+";
if (strpos($_ENV["OS"], "Windows_NT") !== false) $mode .= "b";
// create the file if it doesn't exists
if (!file_exists($file))
{
$temp_handle = fopen($file, "a");
fclose($temp_handle);
}
// create the file handle
$handle = fopen($file, $mode);
// read the file into an array
$file_contents = file($file);
if (filesize($file) > 0)
{
// clean up the contents of the file
foreach ($file_contents as $line)
{
// filter out all whitespaces
$clean_line = trim($line);
if (strlen($clean_line) > 0)
$new_contents[] = $clean_line;
}
// clear the file
ftruncate($handle, 0);
// filter out duplicate addresses from the array
// before writing to file
@array_unique($new_contents);
// write the new contents to file
foreach ($new_contents as $line)
fwrite($handle, "$line\r\n");
}
// append the new address to the file if it doesn't already exist
if (@!in_array($_SERVER['REMOTE_ADDR'], $new_contents))
fwrite($handle, "{$_SERVER['REMOTE_ADDR']}\r\n");
// detroy the file handle
fclose($handle);
Sorry if it's sloppy, but it's late...and I'm tired.