I am trying to get country and city using ip address in php.My code is given below.
我得到了输出。任何机构为这些问题提供任何解决方案?我得到了这样的输出IP地址: - 127.0.0.1 城市: - 没有定义 国家: - 未定义
<?php
/*Get user ip address*/
$ip_address=$_SERVER['REMOTE_ADDR'];
/*Get user ip address details with geoplugin.net*/
$geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip_address;
$addrDetailsArr = unserialize(file_get_contents($geopluginURL));
/*Get City name by return array*/
$city = $addrDetailsArr['geoplugin_city'];
/*Get Country name by return array*/
$country = $addrDetailsArr['geoplugin_countryName'];
/*Comment out these line to see all the posible details*/
/*echo '<pre>';
print_r($addrDetailsArr);
die();*/
if(!$city){
$city='Not Define';
}if(!$country){
$country='Not Define';
}
echo '<strong>IP Address</strong>:- '.$ip_address.'<br/>';
echo '<strong>City</strong>:- '.$city.'<br/>';
echo '<strong>Country</strong>:- '.$country.'<br/>';
?>
答案 0 :(得分:1)
您需要从Internet上的IP地址发出请求。从localhost环回网络接口发送它不会为服务器提供在任何数据库中具有位置的IP地址。