0

我正在尝试在我的 joomla 2.5 安装中实施 Geoip 服务。

我想要的是网站http://www.example.com重定向到法国语言的http://www.example.com/fr

我已经在模板文件的标题中包含了 maxmind geoip。但是重定向在循环中执行。

这就是我执行文件的方式:

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

这是我的重定向脚本:

$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);

我认为因为脚本是从模板执行的,所以每次加载模板文件时都会执行,所以在重定向时它会不断地执行这个脚本。

4

1 回答 1

0

尝试这个:

   $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();
   }

请注意,假定此脚本在 www.example.com/ 和 www.example.com/fr/ 目录中运行

于 2013-03-25T09:55:24.133 回答