What is GeoIP in PHP

The GeoIP extension allows you to find the location of an IP address. City, State, Country, Longitude, Latitude, and other information as all, such as ISP and connection type can be obtained with the help of GeoIP.

To install geoip on linux

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
 gunzip GeoLiteCity.dat.gz
 sudo mkdir -v /usr/share/GeoIP
 sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

sudo apt-get install php5-geoip

Find location Details by IP or domain name

print_r(geoip_record_by_name('example.com')); //It will return following output Array ( [country_code] => US
    [country_code3] => USA
    [country_name] => United States
    [region] => CA
    [city] => Sunnyvale
    [postal_code] => 94089
    [latitude] => 37.4249000549
    [longitude] => -122.007400513
    [dma_code] => 807
    [area_code] => 408


It returns the country code and region name.

<?php $region = geoip_region_by_name('www.example.com'); if ($region) { print_r($region); } ?>


It returns the two letter country code.

<?php $country = geoip_country_code_by_name('www.example.com'); if ($country) { echo 'This host is located in: ' . $country; } ?>


Signature of this method:
string geoip_continent_code_by_name ( string $hostname )

<?php $continent =geoip_continent_code_by_name ( 'www.example.com'); echo $continent; ?>


Signature of this method:
string geoip_country_name_by_name( string $hostname )

<?php $country_full_name=geoip_country_name_by_name ( 'www.example.com'); echo $country_full_name; ?>

Leave a Reply