【问题标题】:PHP header location redirect loopPHP标头位置重定向循环
【发布时间】:2015-11-01 21:09:51
【问题描述】:

我发现了这个 php 地理重定向脚本 here

代码如下:

<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
// create a variable for the country code
$var_country_code = $geoplugin->countryCode;
// redirect based on country code:
if ($var_country_code == "US") {
header('Location: http://domain.com');
}
else if ($var_country_code == "NL") {
header('Location: http://domain.com/nl');
}
else if ($var_country_code == "FR") {
header('Location: http://domain.com/fr');
}
else {
header('Location: http://domain.com/int');
}
?>

我把这段代码放在了我的 index.php 的开头。

我遇到并试图解决的问题是,当我的 IP 是基于美国的重定向脚本时,应该重定向到自身并留在主页上。相反,我得到了一些重定向循环,并且页面无法正确加载。

【问题讨论】:

  • 如果您在美国,并且您转到domain.com,检查器正在与美国进行比较并将您永远重定向到domain.com。尝试为我们添加位置。 domain.com/us
  • 但我想将我们重定向到它自己 (domain.com) 而不是创建单独的域 domain.com/us
  • 然后,尝试检查用户是否访问了不同的域.. if($code=='US' && $_SERVER['REQUEST_URI'])

标签: php loops redirect


【解决方案1】:

如果位置是美国,则无需重定向:

<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
// create a variable for the country code
$var_country_code = $geoplugin->countryCode;
// redirect based on country code:
if ($var_country_code == "US") {
   //header('Location: http://domain.com');
}
else if ($var_country_code == "NL") {
header('Location: http://domain.com/nl');
}
else if ($var_country_code == "FR") {
header('Location: http://domain.com/fr');
}
else {
header('Location: http://domain.com/int');
}
?>

【讨论】:

  • @CarlosGonzález 根据他的问题,重定向代码只写在主域索引页面中。否则,如果每个子文件夹都包含相同的重定向代码,则重定向循环将继续。因为对于您的情况,如果您正在访问 domain.com/fr 并且您的位置是 FR? 而不是您无法访问 fr 文件夹。这将是一个重定向循环。
  • @Chayan 谢谢!它确实有效。也感谢卡洛斯试图帮助我。当我注释掉你建议的那部分时,现在我可以重定向回它自己了。我还从 fr/nl/int 文件夹中的索引页中删除了代码,因为它正在制作重定向循环。但这是在我在这里发布我的问题之前。
  • @MarkoLazar 您还可以将重定向代码放在 fr/nl/int 文件夹的索引页中。但是您必须删除该文件夹的重定向。例如,在 fr 文件夹中注释 header('Location: domain.com/fr'); 这个。这将删除重定向循环。
  • @Chayan 太好了!也会试试的。
  • @Chayan Ofc 你可以!再次感谢 onca!
猜你喜欢
  • 1970-01-01
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多