问题描述
我正在使用geoplugin PHP来获取我网站上访问者的国家和IP,但是它返回的是托管服务器的IP和国家(而不是访问该站点的用户),我使用Hostinger作为托管。
$ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp"));
$country = $ipdat->geoplugin_countryName;
$ip = $ipdat->geoplugin_request;
它在xampp本地主机上正常工作。
解决方法
您的代码行为是正常现象,因为GEO插件站点从脚本请求所在的位置获取IP地址并提供相应的详细信息。如果需要用户/访问者的数据,则需要利用存储在$_SERVER
全局变量中的用户的远程IP,并将此IP传递给GEO插件,以获取其docs
中提到的详细信息。 / p>
代码段:
<?php
$data = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$country = $data['geoplugin_countryName'];
$ip = $data['geoplugin_request'];
echo $country," ",$ip,"<br/>";
print_r($data);