我有一个多语言网站,其中'index.php'位于example.com域。这个index.php应该有这个重定向代码,所以当用户转到'example.com'时,他们会被重定向到该网站的法语版或英文版。
在最简单的形式中,我希望条件语句为:
如果IP位于法国,请重定向到example.com/fr else,如果世界上任何其他地方重定向到example.com/en
我如何使用PHP设置它?
答案 0 :(得分:0)
查看http://www.geoplugin.com/webservices/php并使用缓存存储IP地址,以免每次都发出请求。
<?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));
将导致:
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] => £
[geoplugin_currencySymbol_UTF8] => £
[geoplugin_currencyConverter] => 0.6003
)
请使用国家/地区代码。