简体   繁体   中英

how to get user country name from user ip address in php

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;
}

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; 

Use apinotes external geolocation api

Example:

http://apinotes.com/ipaddress/ip.php?ip=27.62.184.235

URL:  http://apinotes.com/ipaddress/ip.php
Parameter Name: ip
Value:  27.62.184.23 (ipv4 address)

Output:-

Here we display the IP address details in Json query format.

{ 
   "status":"success",
   "ip":"27.62.184.23",
   "country_name":"India",
   "country_code":"IN",
   "country_code3":"IND",
   "region_code":"16",
   "region_name":"Maharashtra",
   "city_name":"Mumbai",
   "latitude":18.975,
   "longitude":72.8258
}

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)

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";
?>

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM