Skip to content

Instantly share code, notes, and snippets.

@zainaali
Last active October 13, 2018 13:52
Show Gist options
  • Save zainaali/3cb0d45a0ae1b479f02d43accc1c42e3 to your computer and use it in GitHub Desktop.
Save zainaali/3cb0d45a0ae1b479f02d43accc1c42e3 to your computer and use it in GitHub Desktop.
Detect a Visitor's Country by his IP Address PHP
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$url = "http://api.wipmania.com/".$ip;
$country = file_get_contents($url);
echo $country;
The variable $country will store the user’s country in ISO code. http://www.danielpinero.com/downloads/countries.txt here is the full list of countries and their ISO codes. Through your IP address
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment