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

时间:2010-12-27 23:18:47

标签: php cookies redirect include setcookie

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

这是我的代码:

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

}   

这应该是一个很容易解决的问题,但是我已经多次改变了这段代码,因为我认为我应该问。

4 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

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

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

例如,如果您的网页为http://example.com/foo.php,请将其发送至http://example.com/foo.php?redirected=1

答案 2 :(得分:0)

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

检查一下: http://nik.chankov.net/2010/01/16/detecting-if-the-cookies-are-enabled-with-php/

答案 3 :(得分:0)

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

如果那时你的检测代码中你可以看到:

  • 没有设置Cookie,
  • 网址参数“cookieset = true”

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