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 に答える