SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    153
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to retrive the correct IP address for locaino information

    hello everybody,

    I'm working on application which uses the IP address of the device to identify location information such as city, state, country. The script works but its picking up the IP address that's assigned by the router.

    how can I get the actually ip address that I need to use in my script to get the correct location information?

    Current setup I have is the internet cable box which is connected to a wireless router and my laptop receives the IP address from the wireless router as such 192.168.0.94.

    I use an external site for retrieve the location information => http://www.geoplugin.net

    And the script I use in my application to get the IP address.

    PHP Code:
    function get_ip_address()   
    {    
    $aa = array 
          ( 
    'HTTP_CLIENT_IP',  
            
    'HTTP_X_FORWARDED_FOR',  
            
    'HTTP_X_FORWARDED',  
            
    'HTTP_X_CLUSTER_CLIENT_IP',  
            
    'HTTP_FORWARDED_FOR',  
            
    'HTTP_FORWARDED',  
            
    'REMOTE_ADDR' 
          
    ); 
          foreach ( 
    $aa as $key)   
          {    if(
    array_key_exists($key$_SERVER) === true)   
            {    foreach (
    explode(','$_SERVER[$key]) as $ip)   
                {    if (
    filter_var($ipFILTER_VALIDATE_IP) !== false)   
                    {    return 
    $ip; }  
                }  
            }  
          }  


  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Use $_SERVER['REMOTE_ADDR']. The reason you get a router-assigned IP is because you're connecting to a local server - probably your own computer. You wont get an external IP that way; it's simply not transmitted. Host the script up on an external site and connect to it, and you'll get your external IP.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    153
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thanks StarLion,

    Ill try that...
    r

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Only ever use $_SERVER['REMOTE_ADDR']! I wish the these "get_ip_address" functions would just die. You should never use anything but 'REMOTE_ADDR'. Unless your server is behind a KNOWN proxy (or load blancer) itself that is properly configured.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •