当用户登陆site.com/redirect.php时,我可以轻松使用此脚本 根据地理IP,他们被重定向到适当的TLD 但是当我在'index.php'中添加此代码时,它会创建一个重定向循环。 你可以帮我修改它,这样它就不会创建循环。现在这个“休息”没有帮助......
<?php
// Next two lines are for Beyond Hosting
// Don't forget to change your-domain
require_once '/home/your-domain/php/Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('/home/your-domain/php/Net/GeoIP.dat');
// Next two lines are for HostGator
require_once 'Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('GeoIP.dat');
try {
$country = $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']);
switch((string)$country) {
case 'AU':
$url = "http://www.site.au";
break;
case 'CA':
$url = "http://www.site.ca";
break;
default:
$url = "http://site.com";
}
header('Location: '.$url);
} catch (Exception $e) {
// Handle exception
}
?>
答案 0 :(得分:1)
您应该在转发前检查用户是否通过本地化URL访问该网站:
<?php
// Next two lines are for Beyond Hosting
// Don't forget to change your-domain
require_once '/home/your-domain/php/Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('/home/your-domain/php/Net/GeoIP.dat');
// Next two lines are for HostGator
require_once 'Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('GeoIP.dat');
try {
$country = $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']);
switch((string)$country) {
case 'AU':
$url = "http://www.site.au";
break;
case 'CA':
$url = "http://www.site.ca";
break;
default:
$url = "http://site.com";
}
if (strpos("http://$_SERVER[HTTP_HOST]", $url) === false)
{
header('Location: '.$url);
}
} catch (Exception $e) {
// Handle exception
}
?>
答案 1 :(得分:0)
use this
header("Location:".$url);
答案 2 :(得分:0)
你可以这样做:(警告,我没有测试过这段代码,但逻辑应该是这样的)
//Inside your try:
$country = $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']);
$serverName = explode('.', $_SERVER['SERVER_NAME']);
$serverCountryCode = $serverName[count($serverName)-1];
if (strtoupper ($serverCountryCode) != $country)) {
$shouldRedirect = true;
switch((string)$country) {
case 'AU':
$url = "http://www.site.au";
break;
case 'CA':
$url = "http://www.site.ca";
break;
default:
if ($serverCountryCode == 'com') {
$shouldRedirect = false;
}
$url = "http://site.com";
}
if ($shouldRedirect) {
header('Location: '.$url);
}
}