This website uses cookies to customize ads and analyze traffic. If you continue browsing, we'll consider you accept their utilization. More information.

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

Web Analytics -

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...

Related Posts
  • How to Get a Visitor's IP Address in Google Analytics

  • How to Add an IP Address to an HTML Form

  • How to Detect a 301 Redirect in PHP

  • How to Detect Tablets in your Website

  • How to Get Likes from a Facebook Page in PHP
  • Send Comment