简体   繁体   English

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

[英]Maxminds GeoIP php Redirect according to country code

I have been working on this for a week now and have tried it with my .htaccess file but even this did not work basically in firefox its saying...我已经为此工作了一个星期,并用我的 .htaccess 文件进行了尝试,但即使这样在 firefox 中基本上也不起作用它的说法......

The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete.该页面未正确重定向 Firefox 检测到服务器正在以一种永远不会完成的方式重定向对此地址的请求。 This problem can sometimes be caused by disabling or refusing to accecp cookies.此问题有时可能是由于禁用或拒绝 accecp cookies 引起的。

and in chrome its saying.....在铬中它的说法.....

This web page has a redirect loop The web page at https://www.website.com/row/index.php has resulted in too many redirects.这个 web 页面有一个重定向循环 位于https://www.website.com/row/index.php的 web 页面导致了太多的重定向。 Clearing your cookies for this site or allowing third-party cookies may fix the problem.清除此站点的 cookies 或允许第三方 cookies 可能会解决此问题。 If not, it is possibly a server configuration issue and not a problem with your computer.如果不是,则可能是服务器配置问题,而不是您计算机的问题。

I uploaded the GeoIP.dat and geoip.inc files from the GeoIP PHP API from Maxminds website to a directory on my hosting then i edited my index.php file with the following php code block....我将 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);
?>

I think it may have something to do with my .htaccess file as it includes this in it....我认为它可能与我的 .htaccess 文件有关,因为它包含了这个....

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

dont know what else to do have already pulled out all my hair on this!不知道还能做什么已经把我所有的头发都拔掉了! many thanks in advace guys!非常感谢你们!

Regards -Phillip问候-菲利普

UPDATED更新

Assuming this code is included in "https://www.website.com/row/index.php"假设此代码包含在“https://www.website.com/row/index.php”中

If I'm visiting this page from BE, the first if statement will be triggered, and bring me to this same page over and over again.如果我从 BE 访问这个页面,第一个 if 语句将被触发,并一遍又一遍地把我带到同一个页面。

update:更新:

Something like this might prevent the loop:这样的事情可能会阻止循环:

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