This article describes how to get country and city information using MaxMind‘s GeoIP technology. You provide the IP address of the visitor and GeoIP can give you country and city information for that IP address.
About MaxMind
Founded in 2002, MaxMind is an industry-leading provider of geolocation and online fraud detection tools. MaxMind provides its geolocation technology through the GeoIP brand.
What is GeoIP?
GeoIP® is the proprietary technology that drives MaxMind’s IP geolocation data and services. GeoIP provides businesses with a non-invasive way to determine geographical and other information about their Internet visitors in real-time. When a person visits your website, GeoIP can determine which country, region, city, postal code, and area code the visitor is coming from. Furthermore, GeoIP can provide information such as longitude/latitude, connection speed, ISP, company name, domain name, and whether the IP address is an anonymous proxy or satellite provider.
They have both free and paid service of GeoIP. This article uses the free version of GeoIP.
Get country name and country code by IP address
You need to include two files: geoip.inc and GeoIP.dat
Download: geoip.inc | GeoIP.dat
/**
* Lookup the country by IP Address
* This will only fetch country code and country name
*/
include("include/geoip.inc");
$gi = geoip_open("include/GeoIP.dat", GEOIP_STANDARD);
echo "Getting Country Code and Name by IP Address <br /><br />";
$ip = "24.24.24.24";
echo "IP: ". $ip . "<br />";
echo "Country Code: " . geoip_country_code_by_addr($gi, $ip) . "<br />" .
"Country Name: " . geoip_country_name_by_addr($gi, $ip) . "<br /><br />";
$ip = "80.24.24.24";
echo "IP: " . $ip . "<br />";
echo "Country Code: " . geoip_country_code_by_addr($gi, $ip) . "<br />" .
"Country Name: " . geoip_country_name_by_addr($gi, $ip) . "<br /><br />";
geoip_close($gi);
Get Country and City detail by IP address
You can get Country, Region, City name and code, Postal code, Latitude, Longitude, Metro code and Area code by IP address.
You need to include three files: geoipcity.inc, geoipregionvars.php and GeoLiteCity.dat
Download: geoipcity.inc | geoipregionvars.inc | GeoLiteCity.dat
/**
* Querying against GeoIP/Lite City
* This will fetch country along with city information
*/
include("include/geoipcity.inc");
include("include/geoipregionvars.php");
$giCity = geoip_open("include/GeoLiteCity.dat",GEOIP_STANDARD);
$ip = "12.215.42.19";
$record = geoip_record_by_addr($giCity, $ip);
echo "Getting Country and City detail by IP Address <br /><br />";
echo "IP: " . $ip . "<br /><br />";
echo "Country Code: " . $record->country_code . "<br />" .
"Country Code3: " . $record->country_code . "<br />" .
"Country Name: " . $record->country_name . "<br />" .
"Region Code: " . $record->region . "<br />" .
"Region Name: " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "<br />" .
"City: " . $record->city . "<br />" .
"Postal Code: " . $record->postal_code . "<br />" .
"Latitude: " . $record->latitude . "<br />" .
"Longitude: " . $record->longitude . "<br />" .
"Metro Code: " . $record->metro_code . "<br />" .
"Area Code: " . $record->area_code . "<br />" ;
geoip_close($giCity);
Get timezone by country and region code
You can get timezone for any country using the country code. You can also use region code for getting region wise timezone as well.
You need to include one file: timezone.php
Download: timezone.php
require("include/timezone.php");
echo get_time_zone("US","WV");
echo "<br />";
echo get_time_zone("NP","");
You can download the php api with sample code files from Maxmind’s GitHub repo: https://github.com/maxmind/geoip-api-php
Hope it helps. Thanks.