当用户连接到我的网站时,我将用户和语言保存在cookie中。但是,当cookie保存时,我将重定向/刷新页面,以便用户获得正确的语言。但是,当用户cookies禁用了cookie时,这会导致无限循环!我以为我会通过编写_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

}   

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

有帮助吗?

解决方案

请勿重定向到同一页面,而是将其重定向 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;

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

其他提示

我认为您误解了包括_once的内容。它不会在多个pageloads上应用,这仅意味着如果您第二次包含文件,则忽略了文件。

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

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

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

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

设置cookie后,重定向用户,然后将“ cookieset = true”参数添加到URL。

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

  • 没有cookie套件,
  • URL参数“ cookieset = true”

然后,您知道用户禁用cookie,因此应重定向到默认语言设置页面。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top