2014-03-04 3 views
0

example.com 도메인에 'index.php'가있는 다국어 사이트가 있습니다. 이 index.php에는이 리디렉션 코드가 있어야 사용자가 'example.com'으로 이동하면 사이트의 프랑스어 또는 영어 버전으로 리디렉션됩니다. 가장 간단한 형태에서IP 위치를 기준으로 PHP 리디렉션

내가 읽을 수있는 조건문을하고 싶습니다 : IP가 프랑스를 기반으로하는 경우

을 example.com/en로 리디렉션 세계 어느 곳의 경우

다른 example.com/fr로 리디렉션

어떻게 PHP를 사용하여 이것을 설정할 수 있습니까?

+0

위치가있는 IP 데이터베이스가 있습니까? – user1844933

+0

@ user1844933 아니요. 왜 이것이 필요한가요? – egr103

답변

0

을보십시오. 캐시를 사용하여 매번 요청하지 않도록 IP 주소를 저장하십시오.

Array 
(
    [geoplugin_request] => My IP 
    [geoplugin_status] => 206 
    [geoplugin_credit] => Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\'http://www.maxmind.com\'>http://www.maxmind.com</a>. 
    [geoplugin_city] => 
    [geoplugin_region] => 
    [geoplugin_areaCode] => 0 
    [geoplugin_dmaCode] => 0 
    [geoplugin_countryCode] => GB 
    [geoplugin_countryName] => United Kingdom 
    [geoplugin_continentCode] => EU 
    [geoplugin_latitude] => 51.5 
    [geoplugin_longitude] => -0.13 
    [geoplugin_regionCode] => 
    [geoplugin_regionName] => 
    [geoplugin_currencyCode] => GBP 
    [geoplugin_currencySymbol] => &#163; 
    [geoplugin_currencySymbol_UTF8] => £ 
    [geoplugin_currencyConverter] => 0.6003 
) 

그래서 국가 코드를 사용

<?php 
error_reporting(-1); 
ini_set("display_errors", "On"); 

$ch = curl_init('http://www.geoplugin.net/php.gp?ip={the IP address, used mine for example}'); 

if (!$ch) { 
    die('Failed CURL'); 
} 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$serverResponse = curl_exec($ch); 

if (!$serverResponse) { 
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    throw new Exception('HTTP error: ' . $code); 
} 

die(print_r($serverResponse)); 

가 발생합니다.

관련 문제