infinte loop when user don't accept cookies
I save my users country and language in a cookie when users connect to my site. But when the cookie is saved i redirect/refresh the page so the user get the correct language. But when a user has cookies disabled this cause an infinite loop! I thought I would fix this by wrinting include_once, but since the site refresh, it include_one every over and over again...
here is my 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"]." ");
}
els开发者_开发技巧e
{
//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
}
It should be a easy fix to this, but I have changed up this code so many times now that I thought I shoud ask.
Do not redirect to the same page, but to another one (or the same one with a GET
parameter).
<?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;
Also note your country detection code is partially wrong (polish users would probably prefer English over German, for example).
I think you're misunderstanding what include_once does. It doesn't apply across multiple pageloads it just means that if you include a file twice the second time you include the file is ignored.
Your check for the cookie is always going to return false if the user has cookies disabled. What you should do is append a variable, say "redirected=1" to the URL before redirecting them. Don't redirect them again if the redirected variable is set, but show an error message or something instead.
So for example if your page is http://example.com/foo.php, send them to http://example.com/foo.php?redirected=1.
You could have another cookie that will tell you if cookies are enabled or not. If they are not, don't do the redirect.
Check this out: http://nik.chankov.net/2010/01/16/detecting-if-the-cookies-are-enabled-with-php/
After setting the cookie, redirect the user but also add a "cookieset=true" parameter to the URL.
If then in your detection code you can see there's:
- No cookie set, and
- the URL Parameter "cookieset=true"
then you know that Cookies are disabled for the user and you should redirect instead to a default language setting page.
精彩评论