首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于IP地址的PHP重定向

问基于IP地址的PHP重定向
EN

Stack Overflow用户
提问于 2014-03-04 18:47:35
回答 1查看 686关注 0票数 0

我有一个多语言的网站与一个'index.php‘坐在example.com域名。这个index.php应该有这个重定向代码,这样当用户转到'example.com‘时,他们会被重定向到网站的法语或英语版本。

在其最简单的形式中,我希望条件语句为:

如果IP位于法国,请重定向至example.com/fr;如果在世界其他任何地方,请重定向至example.com/en

我如何使用PHP来设置它?

EN

回答 1

Stack Overflow用户

发布于 2014-03-04 18:56:39

查看http://www.geoplugin.com/webservices/php并使用缓存存储IP地址,以免每次都发出请求。

代码语言:javascript
复制
    <?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));

将导致:

代码语言:javascript
复制
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
)

所以使用国家代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22169790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档