在IP2Location重定向上设置URL相同页面

问题描述 投票:0回答:1

我将使用按国家/地区划分的重定向访问者,我使用IP2LOCATION

这是我的代码:

require_once('vendor/geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
$var_country_code = $geoplugin->countryCode;
if ($var_country_code == "IN") {
    header('Location: index.php?lang=in');
}
if ($var_country_code == "MY") {
    header('Location: index.php?lang=my');
}

我已经在index.php文件的开头添加了以上代码,问题是页面不断刷新。

例如,index.php将正确地重定向到index.php?lang = in,但它会不断刷新...

我尝试退出;打破;元刷新,session_start();和其他方法。我需要添加会话开始,注册会话并设置cookie,只有一次访问者国家/地区重定向到语言后,用户才可以更改语言。

这是我的语言代码:

session_start();
header('Cache-control: private'); 
if(isSet($_GET['lang']))
{
    $lang = $_GET['lang'];
    // register the session and set the cookie
    $_SESSION['lang'] = $lang;
    setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
    $lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
    $lang = $_COOKIE['lang'];
}
else
{
    $lang = 'en';
}

如何解决此问题?

提前感谢

php session-cookies setcookie ip2location
1个回答
0
投票

将您的位置存储在会话变量中,如果已经设置,则该位置会话无需再次刷新,因此可以打破重定向循环。您不需要语言代码。

session_start();
require_once('vendor/geoplugin.class.php');

if(!$_SESSION['location']){

    $geoplugin = new geoPlugin();
    $geoplugin->locate();
    $var_country_code = $geoplugin->countryCode;

    $_SESSION['location'] = $var_country_code;

    if ($var_country_code == "IN") {

           header('Location: index.php?lang=in');
    }
    if ($var_country_code == "MY") {

            header('Location: index.php?lang=my');
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.