我想要你帮个忙
首先,我从这里下载了MaxMind的GeoIP.dat数据库
然后我从这里下载了geoip.inc
然后我将这两个文件上传到我的页面所在的同一个目录中。
我编辑了我的php页面并在其中编写了这个脚本:
<?php
require_once('geoip.inc');
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
$my_countries = array('us', 'ca', 'gb', 'fr', 'de', 'nl');
if (!in_array(strtolower($country), $my_countries))
{
header('Location: http://"ALL"TRAFFICURLGOESHERE.whatever');
}
else
{
header('Location: http://"SELECTEDCOUNTRIES"URLGOESHERE.whatever');
}
?> 而且效果很好!
但
我想要这样的东西:
其余的国家用户重定向到我想要的相同的URL。
任何人都可以编辑这个php代码与类似2-3个国家的例子,不同的URL,并保留到其他的URL,我想让我可以学习如何添加更多的其他国家(超过4个国家)的代码,因为我真的不擅长php或html。
我也尝试过这个(下面的代码),但是这不起作用。
<?php
require_once('geoip.inc');
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
$my_countries = 'us';
if (strtolower($country) == $my_countries) {
header('Location: example.us');
}
$my_countriess = 'nz';
if (strtolower($country) == $my_countriess) {
header('Location: example.co.nz);
}
$my_countriesss = 'uk';
if (strtolower($country) == $my_countriesss) {
header('Location: example.co.uk');
}
$my_countriessss = 'ca';
if (strtolower($country) == $my_countriessss) {
header('Location: example.ca');
}
?>发布于 2014-02-15 15:36:05
我想要这样的东西:国家A用户重定向到W.其余的国家用户重定向到我想要的相同的URL。
因为您是PHP新手,所以听起来您想使用一系列简单的if语句吗?
if ($country === "us"){
// redirect to US
} else if ($country === "nz"){
// redirect to nz
} else if ($country === "..."){
// continue specifically targeting country codes
} else {
// redirect to everywhere else if none of the above match
}当您开始学习时,您可能需要使用strtolower(),以便字符串与之匹配。
发布于 2014-02-15 15:38:52
您可以为此使用switch ... case。
示例:
switch ($country) {
case 'us':
$location = 'USURL';
break;
case 'uk':
$location = 'UK URL';
break;
case ...
...
default:
$location = 'other countries url';
break;
}
header('Location: $location');有关http://www.php.net/manual/en/control-structures.switch.php语句的更多信息,请参见switch
编辑:顺便说一下,在第二个代码中缺少分号:
header('Location: example.co.nz);应该是
header('Location: example.co.nz');https://stackoverflow.com/questions/21799282
复制相似问题