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.
$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
)
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.
You need to use some kine of Geo IP Service
One free service i found on google: geoplugin. They php snipplets to use their service: geoplugin/php
You can take a look to the maxmind database, and the GeoIP PECL extension.
In my case :
pecl install geoip
"/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 :
And you'll get this kind of output :
Which, in my case, is true : I am indeed in the city of Lyon, FR.
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.
Output:
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.