Ten prosty licznik unikalnych odwiedzin (tzn. z różnych adresów IP) ma jedną poważną wadę - otóż zanim "zaskoczy" blokada IP, można bezproblemowo nabić te kilka dodatkowych "unikalnych" wizyt. Czy w tym kodzie jest jakiś zauważalny błąd, po poprawieniu którego licznik zacząłby działać zgodnie ze swoim założeniem?
Nawet po nabiciu tych kilku dodatkowych wizyt w pliku counter.txt zapisuje się jedynie
-w pierwszej linijce: "0",
-w drugiej linijce: mój numer IP.
<?
/* Just put this code into your index page, and put $unique_ip wherever you want
the counter number.
*/
//-------------------------------------------------------------------begin ip_filter
function ip_filter($ip_array, $ip_number)
{
//This function checks if $ip_number is in an array of IP addresses
//parameters = an array of ip addresses and an IP address
//returns the ip address if the address is not already in the array
//returns 999 if the ip address is in the array
$ip_add=0;
for($n=0;$n<count($ip_array);$n++)
{
if($ip_add != 999 and $n > 0)
{
{
$ip_add=999;
}
else{$ip_add = $ip_number;}
}
}
$unique_ip=count($ip_array); return($ip_add);
}
//-------------------------------------------------------------------end of ip_filter
//------------------------------------------------------------------begin ip_list_read
function ip_list_read($read_file)
{
if($counter_file = fopen($read_file, "r+")) {
$p=0;
while(!feof($counter_file)) {
$p++;
$counter_file_field[$p] = fgets($counter_file, 1024
);
}
}
return($counter_file_field);
}
//------------------------------------------------------------------end of ip_list_read
//-------------------------------------------------------------------begin ip_array_write
function ip_list_write($file_name, $ip_number)
{
//
//
//
// as of now the file that keeps the ip list must be created, but
// it would be easy to change the switch on the fopen() function.
//
//
//
if($counter_file = fopen($file_name, "a")) {
if($ip_number != 999)
{
fwrite($counter_file, $ip_number . "\n"); }
}
}
//------------------------------------------------------------------end of ip_list_write
$z=ip_list_read("counter.txt");
$ip_address=$REMOTE_ADDR;
$y=ip_filter($z,$ip_address);
ip_list_write("counter.txt",$y);
//------------------------------------------------------------------------------------------end of counter code---------------------------------------------
?>