SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a way to get the IP address of someone viewing your page using PHP?

    I tried searching here at sitepoint, just that your keywords have to be three letters long... so ip kinda doesn't work. Also looked through php.net, and that didn't turn up any results either.

  2. #2
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the client's IP address is stored in $REMOTE_HOST.

    John

  3. #3
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that johnn. Another little tiddly bit of information to add to the stack of knowledge.

  4. #4
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just tried it, and it didn't seem to work, although I just found $REMOTE_ADDR and this seems to give me the IP address.

  5. #5
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The information I posted in the previous reply is taken from "building data-base driven website..." by Kevin Yank. Maybe he made a mistake.

    John
    Last edited by johnn; May 20, 2001 at 02:49.

  6. #6
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We can all make mistakes, but from Kevin!! Shock horror!

  7. #7
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It could be just me, but when I'm logging people going to the site everytime, using $REMOTE_ADDR I just get the local IP, ie 127.0.0.1... Perhaps kev was right, or we are both wrong?

  8. #8
    SitePoint Member
    Join Date
    Jan 2001
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Yes and No

    Normally, REMOTE_ADDR would work. However REMOTE_HOST can also work, depending on the server setup.

    Some servers operate the way you described, with the local ip being echoed everywhere, which is not a lot of use.

    You could try $HTTP_X_FORWARDED_FOR
    That might work.

  9. #9
    SitePoint Evangelist AlexC's Avatar
    Join Date
    Oct 2000
    Location
    Sheffield, UK
    Posts
    437
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    would $REMOTE_HOST give me the users hostname?
    Nearly 7 years old!

  10. #10
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The best way to determine what environment variables are available on your server setup is to create a page that calls the built-in function phpinfo(). The output of this function displays a list of all available environment variables, as well as all variables available from the server interface API (e.g. ISAPI or Apache). The variables available to you will be drastically reduced if you use the CGI version of PHP.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  11. #11
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AlexC is right. $REMOTE_HOST will display the user's hostname, if Apache is set to do reverse DNS on everyone who visits. If not, then you will only see $REMOTE_ADDR which is their IP address.

    99% of the time, Apache (or whatever webserver you're running) won't do reverse DNS. So you'll need to do it yourself with gethostbyaddr() ( http://php.net/manual/en/function.gethostbyaddr.php )

  12. #12
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In order to not concern about how the server sets this up, how do we write codes in a script to check $REMOTE_HOST or $REMOTE_ADDR or both to get the correct IP address of someone viewing your page?
    Thanks in advance.

    John
    Last edited by johnn; May 21, 2001 at 01:19.

  13. #13
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I've tried $REMOTE_HOST and $REMOTE_ADDR both locally and on my server. Both do not work. $REMOTE_HOST on both give me nothing, and $REMOTE_ADDR on both give me the local IP.

    As for $HTTP_X_FORWARDED_FOR Camikaze, this gives me two IP addresses.

    203.173.225.149, 203.109.252.13

    From looking at mine I know the first one is mine. Where does the second one come into things. I know the second one is very similar to my ISP's server IP. Would this be the IP of the server requesting the page?

    If this is true then, I can just get the string position of the comma and then select everything up to that comma using substr. However there is probably a more economical way of doing this.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •