PHP Show IP Address of visitors

Discussion in 'OT Technology' started by ITGuy, May 30, 2003.

  1. ITGuy

    ITGuy New Member

    Joined:
    Aug 29, 2002
    Messages:
    31
    Likes Received:
    0
    I'm trying to setup a section of my site that will display the visitor's IP address. Everywhere I look, everyone says to use things like this:

    echo "$REMOTE_ADDR";

    This would be in the php tag. My site will not display the ip address even with this suggested code.

    Does anyone know what I might be doing wrong or of any other code I could try?
     
  2. CruD

    CruD Bah, I'll just hack it

    Joined:
    Oct 25, 2001
    Messages:
    2,796
    Likes Received:
    0
    Location:
    Lakeway, TX
    $ip_address = getenv("REMOTE_ADDR");
    echo("$ip_address");

    or something to that effect...
     
  3. DatacomGuy

    DatacomGuy is moving to Canada

    Joined:
    Oct 14, 2002
    Messages:
    16,546
    Likes Received:
    0
    Location:
    Tampa, FL
    :squint: HEY. YOU'RE the guy who stole the screen name I wanted. :squint::fawk:

    I tried ITGuy, but it was taken so I used DatacomGuy. :rofl:
     
  4. ITGuy

    ITGuy New Member

    Joined:
    Aug 29, 2002
    Messages:
    31
    Likes Received:
    0

    I replaced what I had with your code, and get the following output on the screen:

    Fatal error: Call to undefined function: getnev() in c:\...\...\default.php
     
  5. CruD

    CruD Bah, I'll just hack it

    Joined:
    Oct 25, 2001
    Messages:
    2,796
    Likes Received:
    0
    Location:
    Lakeway, TX
     
  6. ITGuy

    ITGuy New Member

    Joined:
    Aug 29, 2002
    Messages:
    31
    Likes Received:
    0
    LOL... my bad.

    Even with the change, it still doesn't work. It doesn't display anything now. :wtc:

    I even tried using printf("$ip_address") and nothing shows.
    Weird thing is, other php code on the page is executing and displaying fine.

    Going to try a blank page with just the IP address part to see what happens. Here is the code, and it doesn't work either.

    <html>
    <head>
    </head>
    <body>
    Your IP:
    <?
    $ip_address = getenv("REMOTE_ADDR");
    echo("$ip_address");
    ?>
    </body>
    </html>
     
    Last edited: May 30, 2003
  7. CruD

    CruD Bah, I'll just hack it

    Joined:
    Oct 25, 2001
    Messages:
    2,796
    Likes Received:
    0
    Location:
    Lakeway, TX
    I just copied that code and it worked for me. Are you testing this locally? You might have to upload to a host.
     
  8. ITGuy

    ITGuy New Member

    Joined:
    Aug 29, 2002
    Messages:
    31
    Likes Received:
    0
    I have my own server, so that is where I am working.

    I have another set of php code on the same original page that took todays date, compared it to another date and returns the difference (countdown thing) and it is working fine. Could there be something I need to change in my php.ini file to get this function to work?
     
  9. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    First check this out:

    getenv() instructions (with IP address examples)
    http://www.php.net/manual/en/function.getenv.php

    A common approach I take is:
    PHP:
    echo $_SERVER['REMOTE_ADDR'];
    Trick is if you're doing this in a function, you may need to put:
    PHP:
    global $_SERVER;
    $_SERVER is an array of all sorts of nifty info. Depending on if you have globals enabled or not will depend if you need to issue the 'global $_SERVER;' statement.

    I'd probably prefer to go with getenv(), but I remember having problems getting it to work (although this was back with PHP3).
     
  10. ITGuy

    ITGuy New Member

    Joined:
    Aug 29, 2002
    Messages:
    31
    Likes Received:
    0
    Using the $_SERVER method, I get the IP of my Linksys. Is this just because I am trying to see it on my local network?
     
  11. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    This depends on you network setup... Where's your PHP page being hosted at? Internally on your network or with an ISP? If its with an ISP, you should expect to see your external IP address. If its all internal, then usually you should see your machine's IP, but if you have a router in the mix, then its very possible the web server will see your router's IP.
     
  12. ITGuy

    ITGuy New Member

    Joined:
    Aug 29, 2002
    Messages:
    31
    Likes Received:
    0
    This was the case. My server is on my home network behind the Linksys so the page saw the Linksys IP whenever viewing the page from the network. Viewing my page now outside my network, I see my computer's IP.

    Thank for all the insight and help.
     

Share This Page