简体   繁体   English

Joomla GEOIP Maxmind重定向循环

[英]Joomla GEOIP Maxmind redirect loop

I'm trying to implement the Geoip service on my joomla 2.5 installation. 我正在尝试在我的joomla 2.5安装中实现Geoip服务。

What I want is that the website http://www.example.com redirects to http://www.example.com/fr for the france language. 我想要的是将网站http://www.example.com重定向到http://www.example.com/fr以使用法国语言。

I've included the maxmind geoip in the header of the template file. 我已经将maxmind geoip包含在模板文件的标题中。 But the redirect executes in a loop. 但是重定向是在循环中执行的。

This is how I execute the file: 这是我执行文件的方式:

<?php include_once('templates/my_template/geoip/country-redirect.php'); ?>

This is my redirect script: 这是我的重定向脚本:

$gi = geoip_open('templates/my_template/geoip/GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
// prints the country code  your visitor is in
if($country == 'FR')
{
header('Location: http://www.example.com/fr');
exit();
}
else if($country == 'US')
{
header('Location: http://example.com');
exit();
}
// the end
geoip_close($gi);

I think because the script is executed from the template it will execute every time the template file is loaded, so on a redirect it will continiously execute this script. 我认为由于脚本是从模板执行的,因此每次加载模板文件时都会执行,因此在重定向时它将连续执行此脚本。

Try this: 尝试这个:

   $addr = $_SERVER['SERVER_NAME'].dirname( $_SERVER['SCRIPT_NAME']);
   $fr = (($addr == 'www.example.com/fr')||($addr == 'example.com/fr'));
   if((!$fr)&&($country == 'FR')){ // if not at france and country is france
     header('Location: http://www.example.com/fr');
     exit(); // you exit here, so no else required
   }
   if(($fr)&&($country == 'US')){ // if at france and country is US
     header('Location: http://example.com');
     exit();
   }

Note that this script is assumed to run at www.example.com/ and www.example.com/fr/ directories 请注意,假定此脚本运行在www.example.com/和www.example.com/fr/目录中

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

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