Getting location details from IP in PHP

前端 未结 7 1507
傲寒
傲寒 2021-02-03 14:30

Is it possible to get the location details from the users IP Address in PHP.

Any suggestions ??

Thanks

相关标签:
7条回答
  • You can take a look to the maxmind database, and the GeoIP PECL extension.

    In my case :

    • I've installed the extension with "pecl install geoip"
    • And I've downloaded the geolitecity database and copied it to /usr/share/GeoIP/GeoIPCity.dat so it's found by the PECL extension.

    Note there should also be some PEAR package (PEAR::Net_GeoIP) to help you, if you cannot install any PECL extension.


    Once you have installed both of those, you can use this kind of code :

    $ip = '82.229.x.y';     // replace with your IP address
    var_dump(geoip_record_by_name($ip));
    

    And you'll get this kind of output :

    array
      'continent_code' => string 'EU' (length=2)
      'country_code' => string 'FR' (length=2)
      'country_code3' => string 'FRA' (length=3)
      'country_name' => string 'France' (length=6)
      'region' => string 'B9' (length=2)
      'city' => string 'Lyon' (length=4)
      'postal_code' => string '' (length=0)
      'latitude' => float 45.75
      'longitude' => float 4.84999990463
      'dma_code' => int 0
      'area_code' => int 0
    

    Which, in my case, is true : I am indeed in the city of Lyon, FR.

    0 讨论(0)
  • 2021-02-03 15:01

    The initial question remains relevant to many; now late 2016).

    My answer after years of experience with GEO locating is as follows...

    Real reporting of location details beyond "Country" is unreliable. All info is gleaned from the IP# ISP registration and it is that relevant info which is regularly updated and passed on by the major data suppliers. As IP#s are dropped or bought the suppliers must obtain the details about the registration / changes of the businesses concerned (ie ISPs). Most numbers are obtained in "blocks" but not all necessarily relevant to the same server.

    *Many (if not most?) of the smaller privateer websites supplying info to the public use the "Maxmind" FREE db and (improperly) pass that on to the public; read Maxmind caveats. Smaller sites do not have the resources for constant updating of authority registrations.

    The free maxmind and "ip2location" DBs are a smaller subset of their pay-for databases; see their caveats for explanation of what is missing.

    Many IP#s bought in blocks are shared over any number of servers and this leads to info ambiguity. When using a wireless network connection to an ISP, as opposed to a probably stable wired ADSL/cable/blah connection, the errora can be quite startling because of especially broad placement of ISP network servers (relaying); in most cases non-static changing between sessions and perhaps even some during each session.

    As example when using a roaming wireless laptop dongle for ISP connection I am often quoted as being in another state, and that can be (here in AU) anywhere from hundreds to thousands of KMs away from my actual location.

    Can you ... with PHP?

    Using the major couple of DB suppliers (ie ip2location.com) one will have access to available server side scripts which can be used to obtain the necessary info. And rather than a less reliable free DB from them I use ip2location's DB1 to obtain the visitors "Country" only from which I can do what I want to via both Perl and PHP.

    I also AUTO download and decompress, as per usage caveats, the binary db on a monthly basis just after it is updated. Obviously accessing look-ups from ones own site overcomes problems with remote on-demand acquisition and processing.

    For my requirements the ip2location FULL db was considerably cheaper than Maxmind per year. *However the Maxmind FREE db is more extensive than the ip2location FREE DB.

    Note when trying to obtain visitors additional address and state details etc I strongly suggest it is displayed in a way the visitor can make corrections; lest they leave frustrated.

    Regards.

    0 讨论(0)
  • 2021-02-03 15:07
    $ip = '98.229.152.237';
    $xml = simplexml_load_file("http://ipinfodb.com/ip_query.php?ip=$ip");
    print_r($xml);
    

    Output:

    SimpleXMLElement Object
    (
        [Ip] => 98.229.152.237
        [Status] => OK
        [CountryCode] => US
        [CountryName] => United States
        [RegionCode] => 33
        [RegionName] => New Hampshire
        [City] => Manchester
        [ZipPostalCode] => 03103
        [Latitude] => 42.9403
        [Longitude] => -71.4435
        [Timezone] => -5
        [Gmtoffset] => -5
        [Dstoffset] => -4
    )
    
    0 讨论(0)
  • 2021-02-03 15:07

    I recently found PHP GeoIPLocation Library

    Its a single PHP file that you need to include in your PHP script. It automatically searches a well-updated database and returns the country details for you.

    0 讨论(0)
  • 2021-02-03 15:10
     $ip = "66.96.147.144";
        $geoPlugin_array = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $ip) );
        echo '<pre>';
        print_r($geoPlugin_array);
    
    OUTPUT :
    Array
    (
        [geoplugin_request] => 66.96.147.144
        [geoplugin_status] => 200
        [geoplugin_credit] => Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.
        [geoplugin_city] => Burlington
        [geoplugin_region] => MA
        [geoplugin_areaCode] => 781
        [geoplugin_dmaCode] => 506
        [geoplugin_countryCode] => US
        [geoplugin_countryName] => United States
        [geoplugin_continentCode] => NA
        [geoplugin_latitude] => 42.5051
        [geoplugin_longitude] => -71.204697
        [geoplugin_regionCode] => MA
        [geoplugin_regionName] => Massachusetts
        [geoplugin_currencyCode] => USD
        [geoplugin_currencySymbol] => $
        [geoplugin_currencySymbol_UTF8] => $
        [geoplugin_currencyConverter] => 1
    )
    
    0 讨论(0)
  • 2021-02-03 15:14

    Check out the PEAR GeoIP library.

    0 讨论(0)
提交回复
热议问题