ユーザーがサイトに接続すると、ユーザーの国と言語を 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
}
これは簡単に修正できるはずですが、このコードを何度も変更したので、質問する必要があると思いました。