PHP Geoplugin不工作

PHP Geoplugin不工作,php,geolocation,Php,Geolocation,我需要重定向,如果用户来自美国或任何其他国家,不说葡萄牙语。我的代码: require_once('geoplugin.class.php'); $geoplugin = new geoPlugin(); $geoplugin->locate(); // create a variable for the country code $var_country_code = $geoplugin->countryCode; $arrCoun

我需要重定向,如果用户来自美国或任何其他国家,不说葡萄牙语。我的代码:

    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,即使它来自巴西。我的代码有什么问题


请注意,使用

您的重定向是硬编码的,用于将人们引导到/en:

header('Location:http://.$\u SERVER[http\u HOST]./en.$\u SERVER[REQUEST\u URI])

而不是使用返回值
$var\u country\u code

应该看起来更像这样:
header('Location:http://'.$\u SERVER[http\u HOST].//'.$var\u country\u code.//'.$\u SERVER[REQUEST\u URI])

#1 请打开PHP错误和警告的显示:
error\u reporting(E\u ALL)
您可能会从中看到错误,例如:

错误geoPlugin类错误:无法检索数据。使用cURL支持编译PHP,或者在第137行的PHP.ini中启用allow_url_fopen

#2 此代码将变量
$hasEn
设置为
false
(并产生警告),因为您使用的是空分隔符


然后,
$hasEn[1]!='en'
将始终计算为
true
。因此,用户也会被不必要地从
en
站点重定向一次

好的,我修复了这个:
$hasEn=preg_match('/en/',$_SERVER[REQUEST_URI])
if(!in_数组($var_country_code,$arrconrycode)&&&!$hasEn&&$\u COOKIE['redirect']==''){
但是没有意义,因为我的var_country_代码是BR,所以不应该重定向…可能是服务器上的问题,因为我在另一个网站上测试了代码并工作了…如前所述-请打开错误报告,然后您可能会看到原因。可能是服务器配置(例如,curl扩展不可用)。请编辑您的问题以添加报告的错误。
 $hasEn = explode('', $_SERVER[REQUEST_URI]);