我有一个多语言的网站与一个'index.php‘坐在example.com域名。这个index.php应该有这个重定向代码,这样当用户转到'example.com‘时,他们会被重定向到网站的法语或英语版本。
在其最简单的形式中,我希望条件语句为:
如果IP位于法国,请重定向至example.com/fr;如果在世界其他任何地方,请重定向至example.com/en
我如何使用PHP来设置它?
发布于 2014-03-04 18:56:39
查看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
)所以使用国家代码。
https://stackoverflow.com/questions/22169790
复制相似问题