我需要重定向,如果用户是从美国或从任何其他国家,不说葡萄牙语。我的代码:
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
// create a variable for the country code
$var_country_code = $geoplugin->countryCode;
$arrCountryCode = array('BR', 'PT', 'CV', 'GW', 'AO', 'MZ', 'TL', 'ST', 'GQ');
$hasEn = explode('', $_SERVER[REQUEST_URI]);
// $geoplugin = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=".$geoplugin->ip));
// redirect based on country code && if the url has not /en && if is the first view:
if(!in_array($var_country_code, $arrCountryCode) && $hasEn[1] != 'en' && $_COOKIE['redirect'] == '') {
setcookie('redirect', 'true'); //allow to delete /en
header('Location: http://'.$_SERVER[HTTP_HOST].'/en'.$_SERVER[REQUEST_URI]);
}
}
每个人都被重定向到/en,即使它来自巴西。我的代码出了什么问题?
附注:使用geoplugin library。
发布于 2018-08-24 04:06:30
您的重定向是硬编码的,可以将用户定向到/en:
header('Location: http://'.$_SERVER[HTTP_HOST].'/en'.$_SERVER[REQUEST_URI]);
不使用返回值$var_country_code
应该看起来更像这样:header('Location: http://'.$_SERVER[HTTP_HOST].'/'.$var_country_code.'/'.$_SERVER[REQUEST_URI]);
https://stackoverflow.com/questions/51993493
复制相似问题