Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中使用IP地址获取国家和城市_Php - Fatal编程技术网

如何在PHP中使用IP地址获取国家和城市

如何在PHP中使用IP地址获取国家和城市,php,Php,我得到了输出。有没有人能给这些问题提供解决方案?我得到的输出是这样的IP地址:-127.0.0.1 城市:-未定义 国家:-未定义 I am trying to get country and city using ip address in php.My code is given below. 您需要从Internet上的IP地址发出请求。从本地主机环回网络接口发送它不会给服务器一个在任何数据库中都有位置的IP地址。1)使用真实的IP进行尝试,2)阅读MaxMind Lite的文档da

我得到了输出。有没有人能给这些问题提供解决方案?我得到的输出是这样的IP地址:-127.0.0.1 城市:-未定义 国家:-未定义

I am trying  to get country and city using ip address in php.My code is given below.


您需要从Internet上的IP地址发出请求。从本地主机环回网络接口发送它不会给服务器一个在任何数据库中都有位置的IP地址。

1)使用真实的IP进行尝试,2)阅读MaxMind Lite的文档database@symcbean:127.0.0.1是一个真正的IP。@shilna:您误解了IP地址的工作原理以及
$\u服务器['REMOTE\u ADDR']
没有。您需要获得广域网IP才能正常工作。
 <?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/>';
?>