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
Krastley
post
Post #2





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

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


Jest dokładnie tak, jak napisałeś. W manualu znalazłem coś takiego:

"To move to a position before the end-of-file, you need to pass a negative value in offset and set whence to SEEK_END."

To mnie zmyliło, myślałem, że negatywny offset i SEEK_END przeniosą mnie na koniec pliku, ale źle to zrozumiałem.

Wielkie dzięki za pomoc.
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: 10.10.2025 - 09:44