The GeoIP extension allows you to find the location of an IP address. Like the city, state, country, latitude, and longitude.
If you’d like to use the new GeoIP2, MaxMind now offers a pure PHP implementation that doesn’t require a C extension: https://github.com/maxmind/GeoIP2-php.
sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
sudo gunzip GeoLiteCity.dat.gz
sudo mkdir -v /usr/share/GeoIP
sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat
sudo apt-get install php-geoip
Code language: JavaScript (javascript)
And here is an example how to use it with PHP7:
$ip = $_SERVER['REMOTE_ADDR'];
$country = geoip_country_name_by_name($ip);
echo 'The current user is located in: ' . $country;
Code language: PHP (php)