当用户不接受cookie时无限循环

发布于 2024-10-09 19:51:09 字数 5505 浏览 11 评论 0原文

当用户连接到我的网站时,我将用户所在的国家/地区和语言保存在 cookie 中。但是,当保存 cookie 时,我会重定向/刷新页面,以便用户获得正确的语言。但是当用户禁用 cookie 时,这会导致无限循环!我以为我可以通过编写 include_once 来解决这个问题,但自从网站刷新后,它一遍又一遍地包含_one ...

这是我的代码:

lang_set.php

include("php functions\GeoIP\geoipcity.inc");
include("php functions\GeoIP\geoipregionvars.php");
include("php functions\ip.php"); 

if (!isset($_COOKIE['country'])) // om land ikke er registrert (første gang bruker requester siden) 
{   
    $gi = geoip_open("php functions/GeoIP/GeoLiteCity.dat",GEOIP_STANDARD);
    $country = geoip_country_code_by_addr($gi, ip());   

    if ($country == "") { 
        setcookie("country", 'US'); 
        $country = "US"; 
        //reloader og setter språk en_US
        header("Location: ".$_SERVER["REQUEST_URI"]." ");
    }
    else 
    {
        //sett land basert på geoip og reload siden
        setcookie("country", trim($country));
        header("Location: ".$_SERVER["REQUEST_URI"]." ");           
    }

    $country_cookie = false; 

//land ikke satt    
} else {
    //bruker har _COOKIE country
    $country_cookie = true; 

    if ( (!isset($_COOKIE['lang'])) or (!$country_cookie) ){    

        //bruker har country cookie men ikke språk
        //sett språk og reload

        if($country_cookie){
            $country = $_COOKIE['country'];
        }

        if ($country == "NO"){              //Norge
            setcookie("lang", "no_NO");                                 
        }                   
                    /*
                    elseif ($country == "SE" || $country == "FI"){      //Sverige
                        setcookie("lang", "se_SE");                                 
                    }       

                    elseif ($country == "DA" ){         //Danmark
                        setcookie("lang", "dk_DK");                                 
                    }                       
                    */
                    elseif 
                    (
                    $country == "US"                    //Alle engelsktalende land
                    || $country == "AG" || $country == "AI" || $country == "AS" || $country == "AU" || $country == "BE"
                    || $country == "CA" || $country == "FJ" || $country == "GB" || $country == "HK" || $country == "IE"
                    || $country == "JM" || $country == "NF" || $country == "NZ" || $country == "SG" || $country == "UM"
                    || $country == "RW" || $country == "SC"){       

                    setcookie("lang", "en_US");                     
                    }
                    /*
                    elseif ($country == "FR"            //Alle fransktalende land
                    || $country == "AD" || $country == "BI" || $country == "BJ" || $country == "CD" || $country == "CF"
                    || $country == "CG" || $country == "GA" || $country == "GF" || $country == "GN" || $country == "GP"
                    || $country == "HT" || $country == "KM" || $country == "LB" || $country == "MC" || $country == "MG"         
                    || $country == "NC" || $country == "NE" || $country == "PF" || $country == "PM" || $country == "RE"
                    || $country == "TD" || $country == "VA" || $country == "ML" || $country == "MQ"){               

                    setcookie("lang", "fr_FR");     
                    }   

                    elseif ($country == "ES"            //Alle spanske land
                    || $country == "AR" || $country == "MX" || $country == "PA" || $country == "PE" || $country == "PR"
                    || $country == "PY" || $country == "CL" || $country == "CO" || $country == "CR" || $country == "CU"
                    || $country == "DO" || $country == "EC" || $country == "GQ" || $country == "GT" || $country == "HN"         
                    || $country == "NI" || $country == "SV" || $country == "UY" || $country == "VE" ){                  

                    setcookie("lang", "es_ES"); 
                    }   

                    elseif ($country == "DE"            //Alle tyske land
                    || $country == "AT" || $country == "BE" || $country == "CH" || $country == "HU" || $country == "IT"
                    || $country == "LI" || $country == "LU" || $country == "PL"  ){                 

                    setcookie("lang", "de_DE"); 
                    }

                    elseif ($country == "ZH"            //Alle kinesiske land
                    || $country == "CN" || $country == "HK" || $country == "MO" || $country == "SG" || $country == "TW" ){                  

                    setcookie("lang", "zh_ZH"); 
                    }           

                    elseif ($country == "PT" || $country == "BR" ){             
                    setcookie("lang", "pt_PT"); 
                    }                       

                    elseif ($country == "RU" || $country == "MO" ){                 
                    setcookie("lang", "ru_RU"); 
                    }   

                    elseif ($country == "YI" ){                 
                    setcookie("lang", "yi_YI"); 
                    }
                    */

                    //sett default språk engelsk om jeg ikke gjensjender landet
                    else {

                    setcookie("lang", "en_US"); 

                    }

            header("Location: ".$_SERVER["REQUEST_URI"]." "); 
    }// !isset språk

}   

这应该是一个简单的解决方案,但我已经多次更改了此代码现在我想我应该问一下。

I save my users country and language in a cookie when users connect to my site. But when the cookie is saved i redirect/refresh the page so the user get the correct language. But when a user has cookies disabled this cause an infinite loop! I thought I would fix this by wrinting include_once, but since the site refresh, it include_one every over and over again...

here is my code:

lang_set.php

include("php functions\GeoIP\geoipcity.inc");
include("php functions\GeoIP\geoipregionvars.php");
include("php functions\ip.php"); 

if (!isset($_COOKIE['country'])) // om land ikke er registrert (første gang bruker requester siden) 
{   
    $gi = geoip_open("php functions/GeoIP/GeoLiteCity.dat",GEOIP_STANDARD);
    $country = geoip_country_code_by_addr($gi, ip());   

    if ($country == "") { 
        setcookie("country", 'US'); 
        $country = "US"; 
        //reloader og setter språk en_US
        header("Location: ".$_SERVER["REQUEST_URI"]." ");
    }
    else 
    {
        //sett land basert på geoip og reload siden
        setcookie("country", trim($country));
        header("Location: ".$_SERVER["REQUEST_URI"]." ");           
    }

    $country_cookie = false; 

//land ikke satt    
} else {
    //bruker har _COOKIE country
    $country_cookie = true; 

    if ( (!isset($_COOKIE['lang'])) or (!$country_cookie) ){    

        //bruker har country cookie men ikke språk
        //sett språk og reload

        if($country_cookie){
            $country = $_COOKIE['country'];
        }

        if ($country == "NO"){              //Norge
            setcookie("lang", "no_NO");                                 
        }                   
                    /*
                    elseif ($country == "SE" || $country == "FI"){      //Sverige
                        setcookie("lang", "se_SE");                                 
                    }       

                    elseif ($country == "DA" ){         //Danmark
                        setcookie("lang", "dk_DK");                                 
                    }                       
                    */
                    elseif 
                    (
                    $country == "US"                    //Alle engelsktalende land
                    || $country == "AG" || $country == "AI" || $country == "AS" || $country == "AU" || $country == "BE"
                    || $country == "CA" || $country == "FJ" || $country == "GB" || $country == "HK" || $country == "IE"
                    || $country == "JM" || $country == "NF" || $country == "NZ" || $country == "SG" || $country == "UM"
                    || $country == "RW" || $country == "SC"){       

                    setcookie("lang", "en_US");                     
                    }
                    /*
                    elseif ($country == "FR"            //Alle fransktalende land
                    || $country == "AD" || $country == "BI" || $country == "BJ" || $country == "CD" || $country == "CF"
                    || $country == "CG" || $country == "GA" || $country == "GF" || $country == "GN" || $country == "GP"
                    || $country == "HT" || $country == "KM" || $country == "LB" || $country == "MC" || $country == "MG"         
                    || $country == "NC" || $country == "NE" || $country == "PF" || $country == "PM" || $country == "RE"
                    || $country == "TD" || $country == "VA" || $country == "ML" || $country == "MQ"){               

                    setcookie("lang", "fr_FR");     
                    }   

                    elseif ($country == "ES"            //Alle spanske land
                    || $country == "AR" || $country == "MX" || $country == "PA" || $country == "PE" || $country == "PR"
                    || $country == "PY" || $country == "CL" || $country == "CO" || $country == "CR" || $country == "CU"
                    || $country == "DO" || $country == "EC" || $country == "GQ" || $country == "GT" || $country == "HN"         
                    || $country == "NI" || $country == "SV" || $country == "UY" || $country == "VE" ){                  

                    setcookie("lang", "es_ES"); 
                    }   

                    elseif ($country == "DE"            //Alle tyske land
                    || $country == "AT" || $country == "BE" || $country == "CH" || $country == "HU" || $country == "IT"
                    || $country == "LI" || $country == "LU" || $country == "PL"  ){                 

                    setcookie("lang", "de_DE"); 
                    }

                    elseif ($country == "ZH"            //Alle kinesiske land
                    || $country == "CN" || $country == "HK" || $country == "MO" || $country == "SG" || $country == "TW" ){                  

                    setcookie("lang", "zh_ZH"); 
                    }           

                    elseif ($country == "PT" || $country == "BR" ){             
                    setcookie("lang", "pt_PT"); 
                    }                       

                    elseif ($country == "RU" || $country == "MO" ){                 
                    setcookie("lang", "ru_RU"); 
                    }   

                    elseif ($country == "YI" ){                 
                    setcookie("lang", "yi_YI"); 
                    }
                    */

                    //sett default språk engelsk om jeg ikke gjensjender landet
                    else {

                    setcookie("lang", "en_US"); 

                    }

            header("Location: ".$_SERVER["REQUEST_URI"]." "); 
    }// !isset språk

}   

It should be a easy fix to this, but I have changed up this code so many times now that I thought I shoud ask.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空‖城人不在 2024-10-16 19:51:09

不要重定向到同一页面,而是重定向到另一个页面(或带有 GET 参数的同一页面)。

<?php
if (!isset($_COOKIE['lang'])) {
  if (isset($_GET['redirected'])) {
    $lang = getLang();
  } else {
    $_COOKIE['lang'] = getLang();
    header("Location: ".$_SERVER['PHP_SELF']. '?redirected=1'); 
    exit();
  }
} else {
  $lang = $_COOKIE['lang'];
}
echo 'stuff in ' . $lang;

另请注意,您的国家/地区检测代码有部分错误(例如,波兰用户可能更喜欢英语而不是德语)。

Do not redirect to the same page, but to another one (or the same one with a GET parameter).

<?php
if (!isset($_COOKIE['lang'])) {
  if (isset($_GET['redirected'])) {
    $lang = getLang();
  } else {
    $_COOKIE['lang'] = getLang();
    header("Location: ".$_SERVER['PHP_SELF']. '?redirected=1'); 
    exit();
  }
} else {
  $lang = $_COOKIE['lang'];
}
echo 'stuff in ' . $lang;

Also note your country detection code is partially wrong (polish users would probably prefer English over German, for example).

秋意浓 2024-10-16 19:51:09

我认为您误解了 include_once 的作用。它不适用于多个页面加载,它只是意味着如果您两次包含文件,则第二次包含该文件将被忽略。

如果用户禁用了 cookie,您对 cookie 的检查将始终返回 false。您应该做的是在重定向 URL 之前附加一个变量,例如“redirected=1”。如果设置了重定向变量,则不要再次重定向它们,而是显示错误消息或其他内容。

例如,如果您的页面是 http://example.com/foo.php,请将它们发送到 < a href="http://example.com/foo.php?redirected=1" rel="nofollow">http://example.com/foo.php?redirected=1。

I think you're misunderstanding what include_once does. It doesn't apply across multiple pageloads it just means that if you include a file twice the second time you include the file is ignored.

Your check for the cookie is always going to return false if the user has cookies disabled. What you should do is append a variable, say "redirected=1" to the URL before redirecting them. Don't redirect them again if the redirected variable is set, but show an error message or something instead.

So for example if your page is http://example.com/foo.php, send them to http://example.com/foo.php?redirected=1.

无需解释 2024-10-16 19:51:09

您可以有另一个 cookie 来告诉您 cookie 是否启用。如果不是,则不要进行重定向。

看看这个:
http://nik. chankov.net/2010/01/16/Detecting-if-the-cookies-are-enabled-with-php/

You could have another cookie that will tell you if cookies are enabled or not. If they are not, don't do the redirect.

Check this out:
http://nik.chankov.net/2010/01/16/detecting-if-the-cookies-are-enabled-with-php/

倦话 2024-10-16 19:51:09

设置 cookie 后,重定向用户,同时在 URL 中添加“cookieset=true”参数。

如果在您的检测代码中您可以看到:

  • 没有设置 cookie,并且
  • URL 参数“cookieset=true”,

那么您就知道该用户已禁用 Cookie,您应该重定向到默认语言设置页面。

After setting the cookie, redirect the user but also add a "cookieset=true" parameter to the URL.

If then in your detection code you can see there's:

  • No cookie set, and
  • the URL Parameter "cookieset=true"

then you know that Cookies are disabled for the user and you should redirect instead to a default language setting page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文