Search code examples
phpipfwrite

Write file on php


I want to keep ips from visitors and place them on a file.
I tried fwrite() function but I think it is rewrite on the previus ip on file.

Example.

ip.txt is empty.

when I run the write.php Script, on ip.txt I have x.x.x.x ip (my ip)

If My friend runs the write.php Script, on ip.txt I have a.a.a.a ip (friend's ip only)

where is my ip? I want to have on ip.txt file the following:

x.x.x.x   ip1  
a.a.a.a   ip2

Code on write.php is the following.

<?php
$file = fopen("ip.txt","w");
$ip=$_SERVER['REMOTE_ADDR'];
echo fwrite($file,$ip);
fclose($file);
?> 

Solution

  • <?php
    $file = fopen("ip.txt","a");
    $ip=$_SERVER['REMOTE_ADDR'];
    echo fwrite($file,$ip);
    fclose($file);
    ?> 
    

    Look at the manual

    Check what the 2nd parameter means.

    Youve chosen w mode which is an overrwrite mode. Try a mode instead (append)