Page 1 of 2

getting the IP address

Posted: Fri Jan 02, 2004 4:01 pm
by 7up
hi, i have a small problam i was wondering if any one could be so kind as to tell me if there is some way in which i can use PHP to retrive the ip address of all visitors to my site, and some how send this data to a txt file ?? i would really be greatfull if some one could help.

THANKS!

Posted: Fri Jan 02, 2004 4:01 pm
by d3ad1ysp0rk
Why do you want to do this?

Posted: Fri Jan 02, 2004 4:04 pm
by 7up
is it possible?

Posted: Fri Jan 02, 2004 4:06 pm
by m3mn0n
Of course.

IP address variable is $_SERVER['REMOTE_ADDR'];

Check out the [php_man]filesystem[/php_man] functions to learn more about writing to a file.

Posted: Fri Jan 02, 2004 4:08 pm
by 7up
but where will this data go? how will i know if this code is working?

Posted: Fri Jan 02, 2004 4:10 pm
by Straterra
You could either store it in a database or file...

Posted: Fri Jan 02, 2004 4:11 pm
by m3mn0n
7up wrote:but where will this data go? how will i know if this code is working?
You'll know once you create it. :)

Posted: Fri Jan 02, 2004 4:11 pm
by 7up
but how?

Posted: Fri Jan 02, 2004 4:12 pm
by m3mn0n
Did you miss that link up there? hehe

Anyway, click here: [php_man]fwrite[/php_man]()

Posted: Fri Jan 02, 2004 4:20 pm
by 7up
sami, those examples are too complex for me :-(

Posted: Fri Jan 02, 2004 4:27 pm
by JAM
Create test.txt before using this.
Read it, lookup the functions and learn.

Code: Select all

<?php
    $filename = 'test.txt';
    $ip = $_SERVER['REMOTE_ADDR']."\n";

    // Let's make sure the file exists and is writable first.
    if (is_writable($filename)) {

   // In our example we're opening $filename in append mode.
   // The file pointer is at the bottom of the file hence
   // that's where $somecontent will go when we fwrite() it.
   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   }

   // Write $ip to our opened file.
   if (!fwrite($handle, $ip)) {
       echo "Cannot write to file ($filename)";
       exit;
   }

   echo "Success, wrote $ip to file $filename";

   fclose($handle);

} else {
   echo "The file $filename is not writable";
}
?>

Posted: Fri Jan 02, 2004 4:35 pm
by 7up
u make it sound so easy!

Posted: Fri Jan 02, 2004 4:41 pm
by m3mn0n
It is with practice and patience! :)

Posted: Fri Jan 02, 2004 4:55 pm
by 7up
i dont understand how u can say that. i wish some cold explain how i could put this into one pice of code

Posted: Fri Jan 02, 2004 5:18 pm
by 7up
ok, am i suppose to embed this code in a HTML document? or seprate file?