我在这里尝试了很多方法,特别是使用了Geoplugin.net的api。我在加拿大开了一家小商店/网站。我正在尝试重定向从加拿大以外的用户到我们的国际商店网站。如果在美国if去美国唯一的网站。代码托管在加拿大网站上。
<?php
$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?
ip='.$_SERVER['REMOTE_ADDR']));
$countrycode= $a['geoplugin_countryCode'];
if ($countrycode=='US')
header( 'Location: http://USASTORE.com' ) ;
else
header( 'Location: http://internationalstore.com' ) ;
?>使用这个简单的代码,我几乎可以肯定它是正确设置的。出于某种原因,不管是什么,它总是重定向到internationalstore.com。我尝试了美国的IP和CA的IP,结果是一样的。我是不是做错了什么?
发布于 2019-01-04 16:00:49
我要说的是你有问题
$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?
ip='.$_SERVER['REMOTE_ADDR']));它应该是(没有空格或换行)。
$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));这对我来说很好:删除了ip='.$_SERVER['REMOTE_ADDR']),因为在本地服务器上测试,并返回正确的结果。
<?php
$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp'));
$countrycode = $a['geoplugin_countryCode'];
if ($countrycode == 'US'){
var_dump('US');
}elseif ($countrycode == 'AU'){
var_dump('AU');
}else{
var_dump('Other');
}
?>经过测试和工作的代码:
<?php
$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$countrycode = $a['geoplugin_countryCode'];
if ($countrycode == 'US'){
header( 'Location: http://USASTORE.com' );
}elseif ($countrycode == 'AU'){
header( 'Location: http://AUSTORE.com' );
}else{
header( 'Location: http://DEFAULTSTORE.com' );
}
?>发布于 2019-01-04 16:15:25
我认为这方面的一个错误
$a = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?
ip='.$_SERVER['REMOTE_ADDR']));
$countrycode= $a['geoplugin_countryCode'];所以你首先使用var_dump($countrycode);然后你就可以很容易地找到解决方案。
谢谢。
发布于 2019-01-04 15:37:37
试一试
echo "<script>window.location.assign('http://internationalstore.com')</script>";而不是
header( 'Location: http://internationalstore.com' ) ;https://stackoverflow.com/questions/54034712
复制相似问题