繁体   English   中英

Maxminds GeoIP php 根据国家代码重定向

[英]Maxminds GeoIP php Redirect according to country code

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

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

在铬中它的说法.....

这个 web 页面有一个重定向循环 位于https://www.website.com/row/index.php的 web 页面导致了太多的重定向。 清除此站点的 cookies 或允许第三方 cookies 可能会解决此问题。 如果不是,则可能是服务器配置问题,而不是您计算机的问题。

我将 GeoIP.dat 和 geoip.inc 文件从 Maxminds 网站的 GeoIP PHP API 上传到我的主机上的一个目录,然后我用以下 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]

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

问候-菲利普

更新

假设此代码包含在“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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM