Click to See Complete Forum and Search --> : Array_Unique


KashMoney
12-04-2007, 01:03 AM
I am trying to do a hit counter that writes an ip address to a text file. I only want ip's that are unique.

This is what I have;
Code:
<?php
$filename = "hits.txt";

$file = file($filename);
$file = array_unique($file);
$hits = count($file);
echo $hits;

$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);
?>


Then ovbiously the hits.txt logs all of the hit counter unique ip address. The problem I have with this script is that every time I reload the number says at 3 it does add +1 every time. Any Help on that?

Thanks,

KashMoney

Kostas Zotos
12-04-2007, 09:31 AM
Hi,

If i understood well..

I think maybe a new line (or empty line) in your "hits.txt" causes the problem..

For example a new line added with this:
$fcounted = $fstring."\n".getenv("REMOTE_ADDR");

Then when read the file into an array, an extra new line counted each time you run the script.. So maybe use flags in "file" command to filter out the new (or empty) lines.. I mean:

$file = file($filename, FILE_IGNORE_NEW_LINES); // or FILE_SKIP_EMPTY_LINES
$file = array_unique($file);
$hits = count($file);
echo $hits;

//For debbuging purposes may also output (to see array's elements):
print_r($file);

//Your rest code follows here..

Possibly (or as alternative) it would be also useful to run a "for" (or "foreach") loop to check your "$file" array and removes empty strings and/or new lines

Just a suggestion :)

Kostas

KashMoney
12-04-2007, 07:09 PM
Got it solved! Thanks!

Kostas Zotos
12-05-2007, 07:59 AM
Good news!

Just maybe this useful to other people..

I've recreated a similar task and the problem i had (the counter was greater by one than should be) was when there was identical IPs in the log file.

When read the file with "$file = file($filename);" indeed a new line char attached at the end of each IP entry eg. 28.46.250.12\n, 120.36.45.23\n, etc.. Now when there was an identical IP the log file contains entries like: 28.46.250.12\n 120.36.45.23\n 30.40.50.60\n 30.40.50.60 (without the spaces of course) as they added by: $fcounted = $fstring."\n".getenv("REMOTE_ADDR");

However in this case the last entry not contains a new line (\n) at its end. Then the "array_unique($file);" consider for example the: 30.40.50.60\n and 30.40.50.60 (the last entry withou the \n) as different, showing fault incresed by one array elements ("unique" hits).

Using the: "$file = file($filename, FILE_IGNORE_NEW_LINES);" solves the problem as creates an array with only just the elements (with out the new line char at the end of each entry) so duplicated IPs filtered correctly by the "array_unique($file);"

This applied at least in my case..

Bye!

bokeh
12-05-2007, 08:51 AM
Wouldn't it be sensible not to add the IP to the file if it is already there (rather than using array_unique)?

andre4s_y
12-06-2007, 01:12 PM
Same problem there -> Unique Hits script using Flat file not working properly (http://www.webdeveloper.com/forum/showthread.php?t=167907)