Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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取消序列化错误?_Php - Fatal编程技术网

偏移量处的php取消序列化错误?

偏移量处的php取消序列化错误?,php,Php,我试图使用geoplugin在PHP中获取用户gelocation和city 但是,当我运行此代码时,会出现以下错误: Notice: unserialize(): Error at offset 0 of 172 bytes in ndex.php on line 21 这是我的全部代码: $user_ip = getenv('REMOTE_ADDR'); echo $user_ip; $geo = unserialize(file_get_contents("http://www.geop

我试图使用geoplugin在PHP中获取用户gelocation和city

但是,当我运行此代码时,会出现以下错误:

Notice: unserialize(): Error at offset 0 of 172 bytes in ndex.php on line 21
这是我的全部代码:

$user_ip = getenv('REMOTE_ADDR');
echo $user_ip;
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=".$user_ip.""));
$city = $geo["geoplugin_city"];
$region = $geo["geoplugin_regionName"];
$country = $geo["geoplugin_countryName"];
echo "City: ".$city."<br>";
echo "Region: ".$region."<br>";
echo "Country: ".$country."<br>";
我在另一台服务器上使用了这段代码,工作得很好,但在一台新服务器上,我得到了那个错误,我不知道为什么

有人能就这个问题提出建议吗

任何帮助都将不胜感激

编辑:

我将代码编辑为以下内容,但仍然得到相同的错误:

$user_ip = getenv('REMOTE_ADDR');


$geo = file_get_contents("http://www.geoplugin.net/php.gp?ip=".$user_ip."");
$string = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $geo);
$geo = unserialize($string);

$city = $geo["geoplugin_city"];
$region = $geo["geoplugin_regionName"];
$country = $geo["geoplugin_countryName"];
echo "City: ".$city."<br>";
echo "Region: ".$region."<br>";
echo "Country: ".$country."<br>";
$user\u ip=getenv('REMOTE\u ADDR');
$geo=文件\u获取\u内容(“http://www.geoplugin.net/php.gp?ip=“$user_ip.”;
$string=preg\u replace(“!s:(\d+):”(.*?);!se',“'s:”.strlen(“$2”):\“$2\”;”,$geo);
$geo=取消序列化($string);
$city=$geo[“geoplugin_city”];
$region=$geo[“geoplugin_regionName”];
$country=$geo[“geoplugin_countryName”];
回声“城市:.$City.”
“; echo“Region:.$Region.”
“; 回声“国家:.$Country.”
“;
该URL的输出似乎没有正确序列化,至少对于基于英国的IP地址,我通过手动转到URL进行了测试:

其中:

a:18:{s:17:"geoplugin_request";s:12:"81.201.130.8";s:16:"geoplugin_status";i:206;s:16:"geoplugin_credit";s:145:"Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\'http://www.maxmind.com\'>http://www.maxmind.com</a>.";s:14:"geoplugin_city";s:0:"";s:16:"geoplugin_region";s:0:"";s:18:"geoplugin_areaCode";s:1:"0";s:17:"geoplugin_dmaCode";s:1:"0";s:21:"geoplugin_countryCode";s:2:"GB";s:21:"geoplugin_countryName";s:14:"United Kingdom";s:23:"geoplugin_continentCode";s:2:"EU";s:18:"geoplugin_latitude";s:4:"51.5";s:19:"geoplugin_longitude";s:5:"-0.13";s:20:"geoplugin_regionCode";s:0:"";s:20:"geoplugin_regionName";N;s:22:"geoplugin_currencyCode";s:3:"GBP";s:24:"geoplugin_currencySymbol";s:6:"&#163;";s:29:"geoplugin_currencySymbol_UTF8";s:2:"£";s:27:"geoplugin_currencyConverter";s:6:"0.6834";}
a:18:{s:17:“geoplugin_请求”;s:12:“81.201.130.8”;s:16:“geoplugin_状态”;i:206;s:16:“geoplugin_信用”;s:145:“返回的一些数据包括MaxMind创建的GeoLite数据,可从中获取。”s:14:“geoplugin_城市”s:0:;s:16:“geoplugin_地区”s:0:“s:18:“geoplugin_地区代码”s:1:“0”;s:17:“geoplugin(dmaCode:21:“geoplugin_countryCode”s:2:“GB”s:21:“geoplugin_countryName”s:14:“英国”s:23:“geoplugin_洲代码”s:2:“欧盟”s:18:“geoplugin_纬度”s:4:“51.5”s:19:“geoplugin_经度”s:5:-0.13;s:20:“geoplugin_地区代码”s:0:“s:20:“geoplugin_地区名称”N;s:22:“geoplugin_地区代码”s:3:“GBP”;s:24:“geoplugin”currencyCode:“currencyCode:163;“s:29:“geoplugin\u currencySymbol\u UTF8”;s:2:”;s:27:“geoplugin\u currencyConverter”;s:6:“0.6834”}

将其粘贴到在线非序列化程序(例如)中确认它不起作用-我认为这是因为它将磅符号视为2个字符(因为它是UTF-8)-如果我将s:2:“镑”替换为s:1:“镑”,它将正确地非序列化。

是否检查了“获取内容”返回的文件?可能是服务器问题,代码一切正常。我得到了完整的回复:@AndreyMussatov,我做了'var_dump($geo)`我在页面上得到了这样的信息:
bool(false)
@crios,我知道,因为我在另一台服务器上使用了相同的代码,没有任何问题,所以问题来自服务器,我想,但我不知道是什么原因导致了这个问题!考虑使用另一个IP信息服务,例如:为我的英国IP地址完美地工作,它不能提供很多实际数据,因为我能够正确地将IP的结果序列化:“81.201.130.8”。我使用的是5.6.15,所有版本5.5.30及更高版本似乎都存在
geoplugin\u currencycomsymbol\u UTF8
字段问题。问题是完全相同的代码在另一台服务器上绝对可以正常工作!
a:18:{s:17:"geoplugin_request";s:12:"81.201.130.8";s:16:"geoplugin_status";i:206;s:16:"geoplugin_credit";s:145:"Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\'http://www.maxmind.com\'>http://www.maxmind.com</a>.";s:14:"geoplugin_city";s:0:"";s:16:"geoplugin_region";s:0:"";s:18:"geoplugin_areaCode";s:1:"0";s:17:"geoplugin_dmaCode";s:1:"0";s:21:"geoplugin_countryCode";s:2:"GB";s:21:"geoplugin_countryName";s:14:"United Kingdom";s:23:"geoplugin_continentCode";s:2:"EU";s:18:"geoplugin_latitude";s:4:"51.5";s:19:"geoplugin_longitude";s:5:"-0.13";s:20:"geoplugin_regionCode";s:0:"";s:20:"geoplugin_regionName";N;s:22:"geoplugin_currencyCode";s:3:"GBP";s:24:"geoplugin_currencySymbol";s:6:"&#163;";s:29:"geoplugin_currencySymbol_UTF8";s:2:"£";s:27:"geoplugin_currencyConverter";s:6:"0.6834";}