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.

  1. <?
  2. /* Just put this code into your index page, and put $unique_ip wherever you want 
    the counter number.
  3. */
  4.  
  5.  
  6. //-------------------------------------------------------------------begin ip_filter
  7.  function ip_filter($ip_array, $ip_number)
  8.  {
  9. //This function checks if $ip_number is in an array of IP addresses
  10. //parameters = an array of ip addresses and an IP address
  11. //returns the ip address if the address is not already in the array
  12. //returns 999 if the ip address is in the array
  13. $ip_add=0;
  14. for($n=0;$n<count($ip_array);$n++)
  15. {
  16. if($ip_add != 999 and $n > 0)
  17. {
  18. if(strcmp(trim($ip_array[$n]), $ip_number) == 0)
  19. {
  20. $ip_add=999;
  21. }
  22. else{$ip_add = $ip_number;}
  23.  }
  24. }
  25.  global $unique_ip;
  26.  $unique_ip=count($ip_array);
  27.  return($ip_add);
  28.  }
  29.  //-------------------------------------------------------------------end of ip_filter
  30.  
  31.  
  32.  
  33.  //------------------------------------------------------------------begin ip_list_read
  34.  function ip_list_read($read_file)
  35.  {
  36.  
  37. if($counter_file = fopen($read_file, "r+"))
  38. {
  39.  
  40. $p=0;
  41. while(!feof($counter_file))
  42. {
  43. $p++;
  44. $counter_file_field[$p] = fgets($counter_file, 1024);
  45.  
  46. }
  47. }
  48.  
  49. fclose($counter_file);
  50. return($counter_file_field);
  51. }
  52. //------------------------------------------------------------------end of ip_list_read
  53.  
  54.  
  55. //-------------------------------------------------------------------begin ip_array_write
  56. function ip_list_write($file_name, $ip_number)
  57. {
  58. //
  59. //
  60. //
  61. //  as of now the file that keeps the ip list must be created, but
  62. // it would be easy to change the switch on the fopen() function.
  63. //
  64. //
  65. //
  66.  
  67. if($counter_file = fopen($file_name, "a"))
  68. {
  69. if($ip_number != 999)
  70. {
  71. fwrite($counter_file, $ip_number . "\n");
  72. }
  73. }
  74.  
  75. fclose($counter_file);
  76. }
  77. //------------------------------------------------------------------end of ip_list_write
  78.  
  79.  
  80. $z=ip_list_read("counter.txt");
  81. $ip_address=$REMOTE_ADDR;
  82. $y=ip_filter($z,$ip_address);
  83. ip_list_write("counter.txt",$y);
  84.  
  85.  
  86.  
  87. //------------------------------------------------------------------------------------------end of counter code---------------------------------------------
  88. ?>