getting the IP address

Looking for volunteers to join your project? Need help with a script but can't afford to pay? Want to offer your services as a volunteer to build up your portfolio? This is the place for you...

Moderator: General Moderators

7up
Forum Newbie
Posts: 18
Joined: Tue Dec 09, 2003 12:47 pm

getting the IP address

Post 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!
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Why do you want to do this?
7up
Forum Newbie
Posts: 18
Joined: Tue Dec 09, 2003 12:47 pm

Post by 7up »

is it possible?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
7up
Forum Newbie
Posts: 18
Joined: Tue Dec 09, 2003 12:47 pm

Post by 7up »

but where will this data go? how will i know if this code is working?
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

You could either store it in a database or file...
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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. :)
7up
Forum Newbie
Posts: 18
Joined: Tue Dec 09, 2003 12:47 pm

Post by 7up »

but how?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Did you miss that link up there? hehe

Anyway, click here: [php_man]fwrite[/php_man]()
7up
Forum Newbie
Posts: 18
Joined: Tue Dec 09, 2003 12:47 pm

Post by 7up »

sami, those examples are too complex for me :-(
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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";
}
?>
7up
Forum Newbie
Posts: 18
Joined: Tue Dec 09, 2003 12:47 pm

Post by 7up »

u make it sound so easy!
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

It is with practice and patience! :)
7up
Forum Newbie
Posts: 18
Joined: Tue Dec 09, 2003 12:47 pm

Post 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
7up
Forum Newbie
Posts: 18
Joined: Tue Dec 09, 2003 12:47 pm

Post by 7up »

ok, am i suppose to embed this code in a HTML document? or seprate file?
Post Reply