简体   繁体   English

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

[英]Set URL Same Page on IP2Location redirect

I'm going to use Redirect Visitors by Country, I use IP2LOCATION我将使用按国家/地区重定向访问者,我使用IP2LOCATION

This is my code:这是我的代码:

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');
}

I've added this above code in the head of my index.php file, the problem is the page keep refreshing continuously.我在 index.php 文件的头部添加了上面的代码,问题是页面不断刷新。

The index.php will redirect correctly to index.php?lang=in for example, but it keeps refreshing...例如, index.php 将正确重定向到 index.php?lang=in,但它会不断刷新...

I've tried exit;我试过退出; break;休息; meta refresh, session_start();元刷新,session_start(); and other methods.和其他方法。 I need to add session start, register the session and set the cookie, only one time that after redirect to the language by visitor country, user will be able to change language after that.我需要添加 session 启动,注册 session 并设置 cookie,只有一次在访问者国家重定向到语言后,用户将能够更改语言。

And here is my language code:这是我的语言代码:

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';
}

How to solve this issue?如何解决这个问题?

Thanks in advance提前致谢

Store your location in a session variable and if it's already set, then the location session won't need to be refreshed again hence breaking the redirect loop.将您的位置存储在 session 变量中,如果它已经设置,则位置 session 不需要再次刷新,因此会破坏重定向循环。 You don't need the language code.您不需要语言代码。

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');
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM