【问题标题】:infinte loop when user don't accept cookies用户不接受 cookie 时的无限循环
【发布时间】:2010-12-28 07:30:38
【问题描述】:

当用户连接到我的网站时,我将用户的国家和语言保存在 cookie 中。但是当保存 cookie 时,我重定向/刷新页面,以便用户获得正确的语言。但是当用户禁用 cookie 时,这会导致无限循环!我想我会通过写 include_once 来解决这个问题,但是自从网站刷新后,它一次又一次地 include_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

}   

这应该很容易解决,但是我已经多次更改此代码,以至于我想我应该问。

【问题讨论】:

  • 在开始之前确保没有会话,如果有,停止。如果没有,请创建会话并尝试创建 cookie。
  • 你应该使用 in_array 函数而不是一堆 ||

标签: php cookies redirect include setcookie


【解决方案1】:

不要重定向到同一个页面,而是重定向到另一个页面(或带有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;

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

【讨论】:

    【解决方案2】:

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

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

    例如,如果您的页面是http://example.com/foo.php,请将它们发送到http://example.com/foo.php?redirected=1

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 该cookie也不会被存储。
      • 是的,如果它没有被存储,不要重定向。其他的可能不会被存储,因为用户尚未访问该站点或设置他们的偏好。如果 cookie 无论如何都可以工作,则应该是这个。
      【解决方案4】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 2015-04-16
        • 2016-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-17
        • 1970-01-01
        • 1970-01-01
        • 2018-10-11
        相关资源
        最近更新 更多