Getting location information from IP in PHP

Is it possible to get location data from users IP address in PHP.

Any suggestions

thanks

+6
php geoip ip-geolocation
source share
6 answers
$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 ) 
+6
source share

You need to use several Geo IP Service kin

One free service I found on google: geoplugin . They php snipplets use their services: geelugin / php

+4
source share

You can look at the maxmind database , and the GeoIP PECL Extension .

In my case:

  • I installed the extension using < pecl install geoip "
  • And I downloaded the geolithic base and copied it to /usr/share/GeoIP/GeoIPCity.dat to find it using the PECL extension.

Note that there must also be some PEAR package ( PEAR::Net_GeoIP ) if you cannot install the PECL extension.


Once you have installed both of them, you can use the following code:

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

And you get the following 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.

+3
source share

Check out the PEAR GeoIP library .

+1
source share

The initial question remains relevant for many; at the end of 2016).

My answer after many years of experience working with a GEO location is as follows:

Real location reporting outside the Country is unreliable. All information is gleaned from IP # ISP registration, and this is relevant information that is regularly updated and shared by major data providers. As you remove or purchase IP packets, suppliers should receive information about registration / changes to their respective enterprises (for example, Internet providers). Most of the numbers are obtained in “blocks”, but not all of them are necessarily related to the same server.

* Many (if not most?) Small private websites that provide information to the public use the "Maxmind FREE DB" and (incorrectly) pass this on to the public; read the Maxmind disclaimers. Smaller sites do not have the resources to constantly update credentials.

The free maxmind and "ip2location" databases are a smaller subset of their payroll databases; see their reservations for an explanation of what is missing.

Many IP addresses purchased in blocks are divided into any number of servers, and this leads to ambiguity of information. When using a wireless network connection to an Internet service provider, unlike the probably stable ADSL / cable / bla wired connection, the error can be very striking due to the especially wide distribution of ISP network servers (relaying); in most cases, non-static changes between sessions, and possibly even some during each session.

As an example, when using a roaming laptop laptop to connect to an Internet service provider, I am often quoted as being in a different state, and it could be (here in AU) anywhere from hundreds to thousands of KM from my actual location.

Can you ... with PHP ?

Using the main pair of database providers (for example, ip2location.com), you will have access to available server-side scripts that can be used to obtain the necessary information. And instead of getting a less reliable free database from them, I use ip2location DB1 to get "Country" visitors from which I can do what I want through Perl and PHP.

I also automatically download and decompress, as in the case of use, binary db on a monthly basis immediately after updating it. Obviously, viewing access from your own site eliminates problems with remote acquisition and on-demand processing.

For my requirements, ip2location FULL db was significantly cheaper than Maxmind per year. * However, Maxmind FREE db is more extensive than the free version of IB2location FREE.

Pay attention when you try to get additional addresses and data about visitors, etc. I highly recommend that it be displayed in such a way that the visitor can make corrections; so that they are not upset.

Sincerely.

+1
source share
  $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
source share

All Articles