Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Problem z zapisem adresu IP do pliku
Krastley
post
Post #1





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 22.12.2010

Ostrzeżenie: (0%)
-----


Mam taką funkcję:

  1. public static function uniquereader($counter_file) {
  2.  
  3. $user_addr = $_SERVER['REMOTE_ADDR'];
  4.  
  5. if (!is_file($counter_file)) {
  6. $counter_file_stream = fopen($counter_file, 'wb');
  7. fwrite($counter_file_stream, $user_addr);
  8. fclose($counter_file_stream);
  9. }
  10.  
  11. if (!strpos(file_get_contents($counter_file), $user_addr)) {
  12. $counter_file_stream = fopen($counter_file, 'r+b');
  13. fseek($counter_file_stream, -1, SEEK_END);
  14. fwrite($counter_file_stream, "\n".$user_addr);
  15. fclose($counter_file_stream);
  16. }
  17.  
  18. $counter = sizeof(file($counter_file));
  19.  
  20. return $counter;
  21. }


Jak widać, jeżeli nie ma pliku na serwerze, powinien zostać utworzony, a w nim powinien zostać zapisany adres IP. Ale kiedy plik jest tworzony po raz pierwszy, znajduje się w nim coś takiego:


127.0.0.
127.0.0.1


i funkcja zwraca wartość 2. Czy ktoś może mi powiedzieć, dlaczego?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
!*!
post
Post #2





Grupa: Zarejestrowani
Postów: 4 298
Pomógł: 447
Dołączył: 16.11.2006

Ostrzeżenie: (0%)
-----


  1. function ipFile($file)
  2. {
  3. $user_addr = $_SERVER['REMOTE_ADDR'];
  4.  
  5. if(!file_exists($file))
  6. {
  7. file_put_contents($file, $user_addr."\n", FILE_APPEND | LOCK_EX);
  8. return count(file($file));
  9. }
  10.  
  11. if(!stristr(file_get_contents($file), $user_addr))
  12. {
  13. file_put_contents($file, $user_addr."\n", FILE_APPEND | LOCK_EX);
  14. return count(file($file));
  15. }
  16. return count(file($file));
  17. }

Zwraca 2, ponieważ są 2 linijki.

Ten post edytował !*! 26.07.2012, 19:35:32
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.12.2025 - 11:38