How to Detect a Visitor's Country by his IP Address

You can detect a user's country using his IP address with the following code:

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
   $ip = $_SERVER['HTTP_CLIENT_IP'];}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
} else {
   $ip = $_SERVER['REMOTE_ADDR'];
   $url = "".$ip;
   $country = file_get_contents($url);
   echo $country;

The variable $country will store the user’s country in ISO code. Download here the full list of countries and their ISO codes. Through your IP address, we can tell...

Contribute with a comment comment0 Comments
mode_editContribute with a comment