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,007
  • 25
  • 88
  • 136
Ghulam Abbas
  • 504
  • 2
  • 8
  • 22

4 Answers4

10

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,750
  • 3
  • 25
  • 30
  • 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
  • 119
  • 1
  • 7
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
  • 502
  • 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
  • 941
  • 1
  • 7
  • 24