Working with IP Addresses

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Right now I'm using the following function (found on the web) to retrieve a user's IP Address.

PHP Code: [ Select ]
function get_ip()
{
    if(!empty($_SERVER['HTTP_CLIENT_IP']))
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
 
    return $ip;
}
  1. function get_ip()
  2. {
  3.     if(!empty($_SERVER['HTTP_CLIENT_IP']))
  4.     {
  5.         $ip = $_SERVER['HTTP_CLIENT_IP'];
  6.     }
  7.     elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
  8.     {
  9.         $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  10.     }
  11.     else
  12.     {
  13.         $ip = $_SERVER['REMOTE_ADDR'];
  14.     }
  15.  
  16.     return $ip;
  17. }


If anyone knows a better way, please tell me about it here.


My question though, is that I want to determine where the IP is located. Like United States or UK or where... not one of those IP lookup services, but so I would be able to put 'United States' in the database, or something like it.

Anyone knows how I can do that? Thanks.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I solved the IP Location thing. Here is what I'm using.

PHP Code: [ Select ]
function ip_location($ip)
{
    // Loading the global XML file
    $info = (array) simplexml_load_file("http://ipinfodb.com/ip_query.php?ip={$ip}&timezone=true");
   
    // Returing the requested variable to the user
    return $info;
}
  1. function ip_location($ip)
  2. {
  3.     // Loading the global XML file
  4.     $info = (array) simplexml_load_file("http://ipinfodb.com/ip_query.php?ip={$ip}&timezone=true");
  5.    
  6.     // Returing the requested variable to the user
  7.     return $info;
  8. }


The information it returns would look like:
Code: [ Select ]
Array
(
  [Ip] => 65.68.100.18
  [Status] => OK
  [CountryCode] => US
  [CountryName] => United States
  [RegionCode] => 20
  [RegionName] => Kansas
  [City] => Lawrence
  [ZipPostalCode] => SimpleXMLElement Object
    (
    )

  [Latitude] => 38.9555
  [Longitude] => -95.2785
  [TimezoneName] => America/Chicago
  [Gmtoffset] => -18000
  [Isdst] => 1
)
  1. Array
  2. (
  3.   [Ip] => 65.68.100.18
  4.   [Status] => OK
  5.   [CountryCode] => US
  6.   [CountryName] => United States
  7.   [RegionCode] => 20
  8.   [RegionName] => Kansas
  9.   [City] => Lawrence
  10.   [ZipPostalCode] => SimpleXMLElement Object
  11.     (
  12.     )
  13.   [Latitude] => 38.9555
  14.   [Longitude] => -95.2785
  15.   [TimezoneName] => America/Chicago
  16.   [Gmtoffset] => -18000
  17.   [Isdst] => 1
  18. )


Hope that helped someone out.



About that function to figure out the user's IP... is it still a good one? Or should I go look for something else?
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

For the IP its pretty much the best you can do with php.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Alright. Thanks
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

SpooF wrote:
For the IP its pretty much the best you can do with php.

Alright... are there any better solutions in other languages that I would be able to use?

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 95 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.