views:

100

answers:

3

Why cant I change cookie?

If you chose a language you cant change. You have to empty your cookies if you want to change language. Why is that?

if (isset($_GET['setLang']) && $_GET['setLang'] == 'en' 
|| isset($_COOKIE['setLang']) && $_COOKIE['setLang'] == 'en') {
setcookie("setLang", 'en', time()+(3600*12)); //expires in 12 hours
include('language/en/common.php');
}


elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'se' 
|| isset($_COOKIE['setLang']) && $_COOKIE['setLang'] == 'se') {
setcookie("setLang", 'se', time()+(3600*12)); //expires in 12 hours
include('language/se/common.php');
}

else if (isset($_GET['setLang']) && $_GET['setLang'] == 'fr' 
|| isset($_COOKIE['setLang']) && $_COOKIE['setLang'] == 'fr') {
setcookie("setLang", 'fr', time()+(3600*12)); //expires in 12 hours
include('language/fr/common.php');
}

// default language is english
else {
  include('language/en/common.php');
}
+3  A: 

You certainly can change cookies. You can't change languages using the logic you have there because the way you've written it, an existing setting in $_COOKIE will always override a setting in $_GET (except for en, where $_GET will be checked first, so right now you should be able to switch to en if you started with another language). You need to do all checks against $_GET first, then all checks against $_COOKIE, if you want to be able to change languages.

chaos
Or better yet, to make things clearer, pick one of the two locations as the "Canonical language source" and have all your language changing code based off of that one source alone. Then you have the other source override. E.g. make `$_GET` the canonical source, so you have a small blurb of `include`'s, and above that if `$_GET` is not present, have the cookie override and set `$_GET` with its default contents.
dcousineau
+1  A: 

The logic hurt my brain, too.

$language = $_GET['setLang'] || $_COOKIE['setLang']) || 'en';
setcookie("setLang", $language, time()+(3600*12));
include('language/' . $language . '/common.php');

Should achieve the same effect and fix your cookie issues (untested, though).

cpharmston
I'm afraid it won't, because PHP's handling of the `||` operator is, not to put too fine a point to it, fucking retarded. You will wind up with `$language` being either `true` or `false`, there. This is one of the design decisions that Zend made to make life easier for inept programmers that most hobbles competent ones.
chaos
Please dont do that.At least have a list of allowed language codes and validate against this, before blindly trusting input from user and using that in an include.
Anti Veeranna
(Well, actually, since the last term is `'en'`, `$language` will always be `true`.)
chaos
A: 

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.

http://in3.php.net/setcookie

adatapost