Ich speichere meine Benutzer Land und Sprache in einem Cookie, wenn Benutzer eine Verbindung zu meiner Website herstellen. Aber wenn der Cookie gespeichert wird, lege ich die Seite um, damit der Benutzer die richtige Sprache erhält. Aber wenn ein Benutzer Cookies deaktiviert hat, verursacht dies eine Endlosschleife! Ich dachte, ich würde dieses Problem beheben, indem wrinting include_once, aber seit dem Ort erfrischen, include_one es jeden immer und immer wieder ...Infinite-Schleife, wenn der Benutzer keine Cookies akzeptiert
hier ist mein 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
}
Es soll eine einfache Lösung, um dies sein , aber ich habe diesen Code so oft geändert, dass ich dachte, ich sollte fragen.
Bevor Sie beginnen, vergewissern Sie sich, dass es keine Sitzung gibt. Wenn ja, stoppen Sie. Wenn nicht, erstellen Sie eine Sitzung und versuchen Sie, den Cookie zu erstellen. – Robert
Sie sollten stattdessen in_array Funktion Bündel von || verwenden –