【问题标题】:Maxminds GeoIP php Redirect according to country codeMaxminds GeoIP php根据国家代码重定向
【发布时间】:2012-04-22 17:29:13
【问题描述】:

我已经为此工作了一个星期,并已使用我的 .htaccess 文件进行了尝试,但即使这在 Firefox 中也基本不起作用...

页面没有正确重定向 Firefox 检测到 服务器正在以某种方式重定向对该地址的请求 永远不会完成。 此问题有时可能是由禁用或拒绝接受 cookie 引起的。

在 chrome 中它的说法.....

这个网页有一个重定向循环网页在 https://www.website.com/row/index.php 导致太多 重定向。清除本网站的 cookie 或允许第三方 cookie 可以解决问题。如果不是,它可能是一个服务器 配置问题,而不是您的计算机的问题。

我从 Maxminds 网站的 GeoIP PHP API 上传 GeoIP.dat 和 geoip.inc 文件到我主机上的一个目录,然后我使用以下 php 代码块编辑了我的 index.php 文件....

<?php
require_once("geoIP/geoip.inc");
$gi = geoip_open('geoIP/GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
// prints the country code  your visitor is in
$my_countriesrow = array('AD','AE','AF','AG','AI'.....ect);
$my_countrieseuro = array('AN','AT','BA','BE','BG','BY','CH'.....ect);
/* $my_country = array('GB','UK'); */
if (!in_array(strtolower($country), $my_countriesrow)) {
header('Location: https://www.website.com/row/index.php');
exit();
}
else if(!in_array(strtolower($country), $my_countrieseuro)){
header('Location: https://www.website.com/euro/index.php');
exit();
}
else {
header('Location: https://www.website.com/index.php');
exit();
}
// the end
geoip_close($gi);
?>

我认为这可能与我的 .htaccess 文件有关,因为它包含了这个......

# Make all requests have www in them
 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^website\.com
 RewriteRule ^(.*)$ https://www.website.com$1 [R=permanent,L]

不知道还能做什么已经把我所有的头发都拔掉了!非常感谢你们!

问候 -菲利普

【问题讨论】:

  • 这个重定向在哪个页面上?不在它重定向到的任何页面上?
  • 它在Harry主网站的index.php上!

标签: php redirect geoip


【解决方案1】:

更新

假设此代码包含在“https://www.website.com/row/index.php”中

如果我从 BE 访问此页面,将触发第一个 if 语句,并一遍又一遍地将我带到同一页面。

更新:

这样的事情可能会阻止循环:

require_once("geoIP/geoip.inc");
$gi = geoip_open('geoIP/GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
$my_countriesrow = array('AD','AE','AF','AG','AI');
$my_countrieseuro = array('AN','AT','BA','BE','BG','BY','CH');

/* if you got redirected, do not redirect again */
if (!isset($_GET['redirected'])) {
    if (!in_array(strtolower($country), $my_countriesrow)) header('Location: https://www.website.com/row/index.php?redirected');
    else if(!in_array(strtolower($country), $my_countrieseuro)) header('Location: https://www.website.com/euro/index.php?redirected');
    else header('Location: https://www.website.com/index.php?redirected');
}

geoip_close($gi);

【讨论】:

  • 是的,我就是这么想的,哈哈,所以我才问它在哪一页——这是菜鸟的错误!
  • 是的,所有网站的所有 index.php 页面中都有相同的代码块,但它仍然没有重定向
  • 菲利普,再看一遍我的回答,我解释说:如果它在所有 index.php 页面中,它将不起作用。
  • 你不能在所有索引中都有它。它正在做的是循环,因为它重定向到该页面,然后下一页再次重定向它,依此类推四个。您需要从着陆页重定向。
猜你喜欢
  • 2015-01-16
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 2012-11-05
  • 2019-04-15
相关资源
最近更新 更多