2017-09-03 11 views
-2

私は、国を基準にしてユーザーをリダイレクトするユニバーサルリンクを作成しようとしています。私はMaxMindを見てきましたが、何かを無料で簡単に探しています。PHP - 国を基準にリダイレクト

サードパーティを使用せずにユーザーの国を少なくとも80%の精度で検出する方法はありますか?

+0

利用可能な国のすべてのIPアドレスを検索し、独自のテーブルに追加してリクエストします。 –

+0

私はあなたがサードパーティーと言っていないことを知っています、これはサードパーティーですが、シンプルで無料です。 http://api.wipmania.com/<ここにIPアドレスを追加してください>。 –

+0

あなたのサイトにCloudFlareを使用していますか?その場合、CloudFlareヘッダーにアクセスすることができます: '$ _SERVER [" HTTP_CF_IPCOUNTRY "]'そうでなければ、IP-> Country関係のコピーが必要です。これらは変更される可能性があります(?) IMOもしあなたがこのコピーを持っていれば、一致する最も速い方法は '192.168.1.0/24'(例えば –

答えて

0

自分で実装する!あなたは国のIPアドレスのテーブルを作る必要があります...

または単に次のプラグインを使用することができます。

あなたはここでGeoPlugin(第三者)

を使用してリダイレクトを実装する場合はGeoPluginのconfig.phpファイル

<?php 

class geoPlugin { 

    //the geoPlugin server 
    var $host = 'http://www.geoplugin.net/php.gp?ip={IP}&base_currency={CURRENCY}'; 

    //the default base currency 
    var $currency = 'USD'; 

    //initiate the geoPlugin vars 
    var $ip = null; 
    var $city = null; 
    var $region = null; 
    var $areaCode = null; 
    var $dmaCode = null; 
    var $countryCode = null; 
    var $countryName = null; 
    var $continentCode = null; 
    var $latitude = null; 
    var $longitude = null; 
    var $currencyCode = null; 
    var $currencySymbol = null; 
    var $currencyConverter = null; 

    function geoPlugin() { 

    } 

    function locate($ip = null) { 

     global $_SERVER; 

     if (is_null($ip)) { 
      $ip = $_SERVER['REMOTE_ADDR']; 
     } 

     $host = str_replace('{IP}', $ip, $this->host); 
     $host = str_replace('{CURRENCY}', $this->currency, $host); 

     $data = array(); 

     $response = $this->fetch($host); 

     $data = unserialize($response); 

     //set the geoPlugin vars 
     $this->ip = $ip; 
     $this->city = $data['geoplugin_city']; 
     $this->region = $data['geoplugin_region']; 
     $this->areaCode = $data['geoplugin_areaCode']; 
     $this->dmaCode = $data['geoplugin_dmaCode']; 
     $this->countryCode = $data['geoplugin_countryCode']; 
     $this->countryName = $data['geoplugin_countryName']; 
     $this->continentCode = $data['geoplugin_continentCode']; 
     $this->latitude = $data['geoplugin_latitude']; 
     $this->longitude = $data['geoplugin_longitude']; 
     $this->currencyCode = $data['geoplugin_currencyCode']; 
     $this->currencySymbol = $data['geoplugin_currencySymbol']; 
     $this->currencyConverter = $data['geoplugin_currencyConverter']; 

    } 

    function fetch($host) { 

     if (function_exists('curl_init')) { 

      //use cURL to fetch data 
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_URL, $host); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.0'); 
      $response = curl_exec($ch); 
      curl_close ($ch); 

     } else if (ini_get('allow_url_fopen')) { 

      //fall back to fopen() 
      $response = file_get_contents($host, 'r'); 

     } else { 

      trigger_error ('geoPlugin class Error: Cannot retrieve data. Either compile PHP with cURL support or enable allow_url_fopen in php.ini ', E_USER_ERROR); 
      return; 

     } 

     return $response; 
    } 

    function convert($amount, $float=2, $symbol=true) { 

     //easily convert amounts to geolocated currency. 
     if (!is_numeric($this->currencyConverter) || $this->currencyConverter == 0) { 
      trigger_error('geoPlugin class Notice: currencyConverter has no value.', E_USER_NOTICE); 
      return $amount; 
     } 
     if (!is_numeric($amount)) { 
      trigger_error ('geoPlugin class Warning: The amount passed to geoPlugin::convert is not numeric.', E_USER_WARNING); 
      return $amount; 
     } 
     if ($symbol === true) { 
      return $this->currencySymbol . round(($amount * $this->currencyConverter), $float); 
     } else { 
      return round(($amount * $this->currencyConverter), $float); 
     } 
    } 

    function nearby($radius=10, $limit=null) { 

     if (!is_numeric($this->latitude) || !is_numeric($this->longitude)) { 
      trigger_error ('geoPlugin class Warning: Incorrect latitude or longitude values.', E_USER_NOTICE); 
      return array(array()); 
     } 

     $host = "http://www.geoplugin.net/extras/nearby.gp?lat=" . $this->latitude . "&long=" . $this->longitude . "&radius={$radius}"; 

     if (is_numeric($limit)) 
      $host .= "&limit={$limit}"; 

     return unserialize($this->fetch($host)); 

    } 


} 

?> 

、ここでは、国を検証するためのコードです。

require_once('config.php'); //The above code 
$geoplugin = new geoPlugin(); 
$geoplugin->locate(); 
// create a variable for the country code 
$var_country_code = $geoplugin->countryCode; 
// redirect based on country code: 
if ($var_country_code == "OM") { 
header('Location: https:www.something.com/ae'); 
} 
else if ($var_country_code == "US") { 
header('Location: https://www.something.com/en'); 
} 
} 

あなたはCheck Here

0

あなたの国コードがわからない場合これは、CloudFlareの持つ可能です。まず、ドメイン上にCloudFlareアカウントを設定し、IPジオロケーションを有効にする必要があります。これにより、CF-IPCountryヘッダーに国コードが挿入されます。次に、リダイレクトにphp switch文を使用することができます。 CloudFlareはいつでもすぐに廃業されませんようなのCloudFlareとして第三者に頼っ

switch($_SERVER["HTTP_CF_IPCOUNTRY"]) { 
    case: "US" 
    header("Location: http://example.com/US"); 
    case: "UK" 
    header("Location: http://example.com/UK"); 
} 

は、最も可能性の高いあなたの最善の策である、とCloudFlareのは、新しいIPアドレスでリストを更新処理します。本当にサードパーティを使用したくない場合は、いつでも$_SERVER["HTTP_CF_IPCOUNTRY"]を別の国コード検出器に置き換えることができます。

+0

)です。ありがとうございます。私は曇りを見ます。ジオロケーションがどれほど正確であるかについての考えはありますか? – Dppal

+0

地理位置に関する個人的な経験はありませんが、IPv4についてはかなり堅牢ですが、IPv6についてはわかりません。 CloudFlareでの私の経験から、あまりにも早くIPv6をかなり堅牢にすると思います。 –

0

私はmrredirector.comと一緒に行きました。私が指定した国に基づいてURLにリダイレクトするユニバーサルリンクを作成することができます。