Home » PHP » Find Country Name By IP Address using PHP

Find Country Name By IP Address using PHP

Use the following Code to get geo location details based on Your IP

$country data=getLocationInfoByIp();

echo $country data['country'];

echo $country data['city'];

function getLocationInfoByIp()
{
$current_ip = @$_SERVER['HTTP_CLIENT_IP'];
$forwardip = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remoteip = @$_SERVER['REMOTE_ADDR'];
$geo_data = array('country'=>'', 'city'=>'');
if(filter_var($current_ip, FILTER_VALIDATE_IP))
{
$ipaddress = $current_ip;
}
elseif(filter_var($forwardip, FILTER_VALIDATE_IP))
{
$ipaddress = $forwardip;
}
else
{
$ipaddress = $remoteip;
}
$your_ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ipaddress));
// print_r($your_ip_data);
if($your_ip_data && $your_ip_data->geoplugin_countryName != null)
{
$geo_data['country'] = $your_ip_data->geoplugin_countryCode;
$geo_data['city'] = $your_ip_data->geoplugin_city;
}
return $geo_data;
}

Leave a Reply

x

Check Also

Export Database using codeigniter

Database Utility Class : CodeIgniter PHP script to download mysql db-codeigniter,Backup MySQL database with CodeIgniter ...