protected function getCountryNameFromIP()
{
$ip = $_SERVER['REMOTE_ADDR'];
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
$answerIP = @file_get_contents("http://api.ipinfodb.com/v3/ip-country/?key=4b585e37503a519a408dc17878e6ec04fa963e1b946c567722538d9431c2d5cb&format=xml&ip=$ip" ,false,$context);
if(isset($answerIP) && $answerIP !="")
{
$theResJ = simplexml_load_string($answerIP);
$last_login_ip_cn = $theResJ->countryName;
/**
* $last_login_ip_cc = $theResJ->countryCode;
* $last_login_ip_rc = $theResJ->regionCode;
* $last_login_ip_rn = $theResJ->regionName;
* $last_login_ip_cp = $theResJ->cityName;
* $last_login_ip_lat = $theResJ->latitude;
* $last_login_ip_lng = $theResJ->longitude;
* $last_login_zip_code= $theResJ->zipCode;
*/
}
else
{
$last_login_ip_cn = "";
/**
* $last_login_ip_cc = "";
* $last_login_ip_rc = "";
* $last_login_ip_rn = "";
* $last_login_ip_cp = "";
* $last_login_ip_lat = "";
* $last_login_ip_lng = "";
* $last_login_zip_code= "";
*/
}
return $last_login_ip_cn;
}
I hope it helps you