Matpatnik Posted November 20, 2007 Share Posted November 20, 2007 Hi guys,I'm trying to set a language using post to keep the same language trough the whole site and set a cookie for the next time they come back.But the problem is it doesn't pass the cookie if statement.I have no clue on how to fix it $lang_supported = array('en','fr');// good for 30 days$endcookie = time()+60*60*24*30;// TODO Problem with cookie, fix it!if (isset($_COOKIE['lang']) && $_COOKIE['lang'] != '') { if (isset($_GET['lang']) && in_array($_GET['lang'], $lang_supported)) { // ------------- change cookie and session language $_SESSION['lang'] = $_GET['lang']; setcookie('lang', $_SESSION['lang'], $endcookie, '/', '.adenak.com', 0, 1); } else { // ---------------------------------------------------------------------------- here I had to set the default which doesn't make sence to me, default was already suposed to be set $_SESSION['lang'] = "en"; setcookie('lang', $_SESSION['lang'], $endcookie, '/', '.adenak.com', 0, 1); }} else if (isset($_SESSION['lang']) && $_SESSION['lang'] != '') { // ----------------------- cookie not supported if (isset($_GET['lang']) && in_array($_GET['lang'], $lang_supported)) { // ------------- change session language $_SESSION['lang'] = $_GET['lang']; } else { $_SESSION['lang'] = "en"; }} else { // -------------------------------------------------------------------------------- set default language // TODO get language from the browser with javascript $_SESSION['lang'] = "en"; setcookie('lang', $_SESSION['lang'], $endcookie, '/', '.adenak.com', 0, 1);} Thank you Link to comment Share on other sites More sharing options...
boen_robot Posted November 20, 2007 Share Posted November 20, 2007 This was previously discussed before in not one, but two other recent topics, and the estabilished pipeline was to first negotiate the language, and only then set the cookie/session properly.Here's a copy: $langCode = '[a-z]{2}(\-[a-z]{2})?';$lang = (ereg($langCode, $_GET['lang']) && is_file("terms-{$_GET['lang']}.htm") ? $_GET['lang'] : (ereg($langCode, $_COOKIE['lang']) && is_file("terms-{$_COOKIE['lang']}.htm") ? $_COOKIE['lang'] : 'en'));setcookie('lang',$lang); In the place of the is_file() check you should place whatever sort of check you believe would detect if you have the specified language. From your sample, I suppose it could be redone as: $lang_supported = array('en','fr');$langCode = '[a-z]{2}(\-[a-z]{2})?';$lang = (ereg($langCode, $_GET['lang']) && in_array($_GET['lang'], $lang_supported) ? $_GET['lang'] : (ereg($langCode, $_COOKIE['lang']) && in_array($_GET['lang'], $lang_supported) ? $_COOKIE['lang'] : 'en'));setcookie('lang',$lang, time()+60*60*24*30); This doesn't use a session, but uses a cookie, which should be sufficient enough. Integration session into the game shouldn't be that hard. Link to comment Share on other sites More sharing options...
Matpatnik Posted November 20, 2007 Author Share Posted November 20, 2007 This was previously discussed before in not one, but two other recent topics, and the established pipeline was to first negotiate the language, and only then set the cookie/session properly.Thank you boen_robot. I know, I didn't looked very fare in the forum I'm not sure what ? and : mean in $lang. it look like a if statement. Is it? if yes, is it faster then a regular if-else? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 20, 2007 Share Posted November 20, 2007 It's just a shorthand way to write an if statement, when it compiles down it's probably the same structure, so there's no speed difference I would imagine. It's quicker to type if you're using an if statement for assignment. It's also the only ternary (three-operand) operator in PHP. It also gives you a little warm and fuzzy feeling when you use it. The line starting with $lang actually uses 2 if/else structures to determine the value of $lang, it's easier to just put it all on one line.http://www.php.net/manual/en/language.oper...parison.ternary Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.