簡體   English   中英

在 IP2Location 重定向上設置 URL 相同頁面

[英]Set URL Same Page on IP2Location redirect

我將使用按國家/地區重定向訪問者,我使用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(); 和其他方法。 我需要添加 session 啟動,注冊 session 並設置 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';
}

如何解決這個問題?

提前致謝

將您的位置存儲在 session 變量中,如果它已經設置,則位置 session 不需要再次刷新,因此會破壞重定向循環。 您不需要語言代碼。

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