首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP Geoplugin不起作用

PHP Geoplugin不起作用
EN

Stack Overflow用户
提问于 2018-08-24 04:03:22
回答 1查看 1.2K关注 0票数 1

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

    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

EN

回答 1

Stack Overflow用户

发布于 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]);

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

https://stackoverflow.com/questions/51993493

复制
相关文章

相似问题

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