SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Addict CeleronXL's Avatar
    Join Date
    Dec 2002
    Location
    United States of America
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Three Ways To Grab An IP

    I know this was posted before, but I can't figure out what to search for because I can't enter the search term "IP"..

    In PHP, what are the three ways to get it (generally separated by OR statements).
    So hold me when I'm here
    Right me when I'm wrong
    Hold me when I'm scared
    Love me when I'm gone.

  2. #2
    Back in Action Winged Spider's Avatar
    Join Date
    Jun 2001
    Location
    outside my mind
    Posts
    900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Type in "php" and "ip" into google. The first 10 pages all have the answer your looking for.

  3. #3
    SitePoint Addict CeleronXL's Avatar
    Join Date
    Dec 2002
    Location
    United States of America
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't find all three ways.. There were three ways, but from what I find on Google, I only get one way..
    So hold me when I'm here
    Right me when I'm wrong
    Hold me when I'm scared
    Love me when I'm gone.

  4. #4
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can only think of $_SERVER['REMOTE_ADDR'] , which holds the visitor's IP.

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $_SERVER['REMOTE_ADDR']
    $_SERVER['HTTP_X_FORWARDED_FOR']

    I can't remember the other one.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  6. #6
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't $_SERVER['HTTP_X_FORWARDED_FOR'] only contain the real IP of the visitor if he uses a proxy and that proxy fills this variable?

  7. #7
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes. my ISP does it. the only way to get my IP on a remote server is with HTTP_X_FORWARDED_FOR
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  8. #8
    SitePoint Addict CeleronXL's Avatar
    Join Date
    Dec 2002
    Location
    United States of America
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok.. So is it..
    PHP Code:
    $ip = (getenv('REMOTE_ADDR') OR $_SERVER['REMOTE_ADDR'] OR $_SERVER['HTTP_X_FORWARDED_FOR']); 
    To get the IP?
    So hold me when I'm here
    Right me when I'm wrong
    Hold me when I'm scared
    Love me when I'm gone.

  9. #9
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    getenv() is nothing different than $_SERVER['REMOTE_ADDR'] .

  10. #10
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also, it would be

    PHP Code:
    $ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; 
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  11. #11
    SitePoint Addict CeleronXL's Avatar
    Join Date
    Dec 2002
    Location
    United States of America
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, I did this...
    PHP Code:
    if (isset($_SERVER)) {
    if (isset(
    $_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ipaddress $_SERVER['HTTP_X_FORWARDED_FOR'];
    $proxy $_SERVER['REMOTE_ADDR'];
    } elseif (isset(
    $_SERVER['HTTP_CLIENT_IP'])) {
    $ipaddress $_SERVER['HTTP_CLIENT_IP'];
    } else {
    $ipaddress $_SERVER['REMOTE_ADDR'];
    }
    } else {
    if (
    getenv('HTTP_X_FORWARDED_FOR')) {
    $ipaddress getenv('HTTP_X_FORWARDED_FOR');
    $proxy getenv('REMOTE_ADDR');
    } elseif (
    getenv('HTTP_CLIENT_IP')) {
    $ipaddress getenv('HTTP_CLIENT_IP');
    } else {
    $ipaddress getenv('REMOTE_ADDR');
    }

    So hold me when I'm here
    Right me when I'm wrong
    Hold me when I'm scared
    Love me when I'm gone.

  12. #12
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why so complicated? $_SERVER is always set, you don't need that if-else clause.
    Btw, what is $_SERVER['HTTP_CLIENT_IP'] ?

  13. #13
    SitePoint Addict CeleronXL's Avatar
    Join Date
    Dec 2002
    Location
    United States of America
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Beats me.. I just took all of that from php.net..
    So hold me when I'm here
    Right me when I'm wrong
    Hold me when I'm scared
    Love me when I'm gone.

  14. #14
    SitePoint Enthusiast M ot U's Avatar
    Join Date
    Jul 2003
    Location
    Earth
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dude whats up with your signature?

  15. #15
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    El Toro, CA (USA)
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A quick google search revlealed it's a 3 doors down song called 'when I'm gone'. Bang.

  16. #16
    SitePoint Enthusiast M ot U's Avatar
    Join Date
    Jul 2003
    Location
    Earth
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a quick brain search revealed that it's a nu-metal song... *cough* retarded.
    M ot U == Master of the Universe

    because i am...

  17. #17
    SitePoint Addict CeleronXL's Avatar
    Join Date
    Dec 2002
    Location
    United States of America
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by M ot U
    a quick brain search revealed that it's a nu-metal song... *cough* retarded.
    Think what you wish, but lay of the sig.

    allset.k.thnx
    So hold me when I'm here
    Right me when I'm wrong
    Hold me when I'm scared
    Love me when I'm gone.

  18. #18
    SitePoint Enthusiast sinatosk's Avatar
    Join Date
    Jul 2003
    Location
    PHP World
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if ( !isset($HTTP_SERVER_VARS) )
    {

    $s =& $_SERVER;

    }
    else
    {

    $s =& $HTTP_SERVER_VARS;

    }

    $http_x_forwarded_for,
    $s['http_x_forwarded_for'],
    $http_client_ip,
    $s['http_client_ip'],
    $http_proxy_user,
    $s['http_proxy_user'],
    $remote_addr,
    $s['remote_addr'],

  19. #19
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    am using this function :

    PHP Code:
      Function Getip() {
       global 
    $HTTP_SERVER_VARS,$HTTP_X_FORWARDED_FOR,$HTTP_CLIENT_IP,$REMOTE_ADDR;
       if (isSet(
    $HTTP_SERVER_VARS)) {
        if (isSet(
    $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])) {
         
    $realip $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
        } elseif (isSet(
    $HTTP_SERVER_VARS["HTTP_CLIENT_IP"])) {
         
    $realip $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
        } else {
         
    $realip $HTTP_SERVER_VARS["REMOTE_ADDR"];
        }
       
       } else {
        if ( 
    getenv'HTTP_X_FORWARDED_FOR' ) ) {
         
    $realip getenv'HTTP_X_FORWARDED_FOR' );
        } elseif ( 
    getenv'HTTP_CLIENT_IP' ) ) {
         
    $realip getenv'HTTP_CLIENT_IP' );
        } else {
         
    $realip getenv'REMOTE_ADDR' );
        }
       }
       return 
    $realip
      } 

  20. #20
    SitePoint Addict CeleronXL's Avatar
    Join Date
    Dec 2002
    Location
    United States of America
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you really need to global $HTTP_SERVER_VARS ?
    So hold me when I'm here
    Right me when I'm wrong
    Hold me when I'm scared
    Love me when I'm gone.

  21. #21
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $HTTP_SERVER_VARS should always be set, even if the version of PHP is new, for backwards compatibility reasons. And you are using global on way too many things, I don't know if it's necessaire for $HTTP_SERVER_VARS, but for the rest it shouldn't be needed. It would definitely not be needed if you use $_SERVER .
    Also I would think that everything you can get with getenv() should already be in $HTTP_SERVER_VARS / $_SERVER .

  22. #22
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CeleronXL
    Do you really need to global $HTTP_SERVER_VARS ?
    this from old code, I think I found it php.net, with new version of php no need for global !

  23. #23
    SitePoint Zealot
    Join Date
    Feb 2003
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The $_SERVER array is always auto-global. But for $HTTP_SERVER_VARS you have to global them in functions etc.

    As has been written, you would only need to check for HTTP_SERVER_VARS vs. getenv, when you are using a really old version of PHP, which is highly unlikely. Most setups I'va come across, support the (newest) $_SERVER, which is now the preferred thing to use.

    Use this function, it'll also check the forwarded user-ip from the proxy, to make sure it's not a local IP. It's over at devshed:
    http://forums.devshed.com/t64009/s23...78bf36073.html

    I would have posted the code, but the crappy posting interface here, will stuff all kinds of other (font etc.-)tags in it.


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
  •