2

当用户登陆 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
}
?>
4

3 回答 3

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
}
?>
于 2013-06-04T14:26:53.110 回答
0
use this  
header("Location:".$url);
于 2013-06-04T14:18:23.180 回答
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);
  }
}
于 2013-06-04T14:23:41.960 回答