5

how to get country name from user IP address

I have IP addresses of user i get the country name of user base on user IP address how it is possible in php ?

function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
       $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}
Matt
  • 14,906
  • 27
  • 99
  • 149
Ghulam Abbas
  • 515
  • 2
  • 10
  • 23

5 Answers5

12

Try this

function ip_details($IPaddress) 
{
    $json       = file_get_contents("http://ipinfo.io/{$IPaddress}");
    $details    = json_decode($json);
    return $details;
}

$IPaddress  =  'Your ip address of user';

$details    =   ip_details("$IPaddress");

//echo $details->city;   
 echo $details->country;  
//echo $details->org;      
//echo $details->hostname; 
Web Artisan
  • 1,870
  • 3
  • 23
  • 33
  • if i want to get currency of country how it is ? – Ghulam Abbas May 09 '16 at 10:12
  • @GhulamAbbas you can try usercountry.com which includes currency in the result. $result = json_decode(file_get_contents("http://usercountry.com/v1.0/json/{$IPaddress}"), true); Then you can just do $result['currency']['code']. – byl83 Aug 10 '17 at 04:12
1

For getting IP address you can use the function given by you or simply you can use

$_SERVER['REMOTE_ADDR']

to get the IP address of the user use as follow:

$ip =  $_SERVER['REMOTE_ADDR'];   // ip = 8.8.8.8
$country = file_get_contents('http://ipinfo.io/8.8.8.8/country'); // for more info visit [enter link description here][1] 
echo $country;  // output = US | UK | IN as per ip (just one country code)
Sunil Kumar
  • 129
  • 1
  • 7
1

Although this post has a correct answer, I have a small modification on it : using curl instead of file_get_contents because :

  1. ipinfo.io documents suggests that.
  2. file_get_contents doesn't have try catch pattern.
  3. You can set a timeout request easily to prevent script waiting long for response.

Note :

Without Token, you will reach the limitation soon, so to gain free 50.000 requests/month (currently) it's better to add token in end of url :

function ip_details($ip) //return country code based on ip
{
    if(empty($ip)) return "";
    
    $url="http://ipinfo.io/{$ip}?token=XXXXX";
    
    //trigger url
    $curl = curl_init();
    
    curl_setopt($curl, CURLOPT_TIMEOUT, 3); // wait 3 seconds to response
    curl_setopt($curl, CURLOPT_URL, $url);
    //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    //curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
       'Content-Type: application/json',
    ));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    
    $json = curl_exec($curl);
    
    if (curl_errno($curl)) {
       return ""; 
    } 
    
    //curl_close($curl);

    $details    = json_decode($json);
    $country = (empty($details->country)) ? "" : $details->country;
    
    return $country;
}

Kranchi
  • 73
  • 1
  • 9
0

You can do this, which includes currency:

<?
function resolveIP($ip) {
  $string = file_get_contents("https://ipsidekick.com/{$ip}");
  $json = json_decode($string);
  return $json;
}

$ip = '17.142.160.59';
$json = resolveIP($ip);

echo "Country name: {$json->country->name}\n";
echo "Currency name: {$json->currency->name}\n";
echo "Public holiday: {$json->currency->holiday}\n";
?>
Hwee-Boon Yar
  • 512
  • 3
  • 13
-1

Get Current country name and country lat long value :

$IPaddress = !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
$details = $this->ip_details("$IPaddress");
$country_name = $details->country_name;

$geocode_stats = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=$country_name&sensor=false");
$output_deals = json_decode($geocode_stats);
$latLng = $output_deals->results[0]->geometry->location;
echo $lat = $latLng->lat;
echo $lng = $latLng->lng;
Rahul Pawar
  • 1,016
  • 1
  • 8
  • 25