Reputation: 35
The following code is the first code in my php page.
$current_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(isset($_REQUEST["lang"])){ //check if different language was selected
$lang = $_REQUEST["lang"];
if($lang == "eng"){
$lang_value = 1;
}else{
$lang_value = 0;
}
setCookie('language',$lang);
setCookie('language_value',$lang_value);
header("Refresh:0; url=".$current_link);
}else{ //if different language was not selected, check if cookie is set with language value
if(isset($_COOKIE["language"])){
$lang = $_COOKIE["language"];
$lang_value = $_COOKIE["language_value"];
}else{ //if cookie with language value is not set, create it now with default language option
setCookie('language','eng');
setCookie('language_value',1);
header("Refresh:0; url=".$current_link);
}
}
The visitor can only choose one of two languages. If a language is chosen, cookies with the chosen language values are created.
If a language is not chosen, the script checks if a cookie with the language value exists, and if so, accesses the cookie values. If a cookie with the language value does not exist, cookies with the default language (English) values are created.
The page is suppose to only refresh when cookies are created and continue the rest of the code if the cookie with the language value exists. However, the page keeps on refreshing even after the cookies were created and are accessible. eg:
echo $_COOKIE["language"]; //will output the selected language value
Not sure if there is something wrong with the logic here, but any help will be appreciated.
Thanks
Upvotes: 2
Views: 469
Reputation: 35
I managed to solve the mystery.
The problem was in the .htaccess file setup, eg:
RewriteRule diploma-golf course.php?lang=eng&courseid=15 [NC])
Since I only saw the 'diploma-golf' part at the end of the url, I never realised that the "lang" value was passed on every time the page refreshed. So because the script received the "lang" value each time, it created a new cookie each time, resulting in the infinite refreshing. Changed the if statement logic and now everything is working as it should.
Upvotes: 1
Reputation: 78
Every request to the page will run this part:
if(isset($_REQUEST["lang"])){
That means that the page will be refreshed because of this unconditional part of your code:
header("Refresh:0; url=".$current_link);
What you might want to do is removing the first Header part and only keep the header part as described in your text. It would look like so:
$current_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(isset($_REQUEST["lang"])){ //check if different language was selected
$lang = $_REQUEST["lang"];
if($lang == "eng"){
$lang_value = 1;
}else{
$lang_value = 0;
}
setCookie('language',$lang);
setCookie('language_value',$lang_value);
#header("Refresh:0; url=".$current_link); ### Removing this line
}else{ //if different language was not selected, check if cookie is set with language value
if(isset($_COOKIE["language"])){
$lang = $_COOKIE["language"];
$lang_value = $_COOKIE["language_value"];
}else{ //if cookie with language value is not set, create it now with default language option
setCookie('language','eng');
setCookie('language_value',1);
header("Refresh:0; url=".$current_link);
}
}
Upvotes: 0
Reputation: 354
You need to add expire and path for the cookie:
setCookie('language',$lang, time() + (86400 * 30), "/"); // 86400 = 1 day
Upvotes: 0