I am trying to configure, as I thought, a simple language switch. I thought I would use PHP cookies, but they do not behave as intended.
I read several cookie guides and looked at a few similar examples here on StackOverflow, but there must be something missing for me because it cannot make it work correctly.
I set the language by passing it as a URL variable (lang = en or lang = ru). Everything seems to be all right. However, the code that I have at that moment that sets the cookie seems to be one step behind, so it doesn't matter initially (I would like it to be "en" by default), then if the user presses "ENG", the button still does not matter, and then, if the user clicks on Russian, the value is displayed as "en", and then, if I press the "ENG" button again, the value is displayed as "ru".
Here is the code I combined:
if( $_GET['lang'] ) { $lang = (string)$_GET['lang']; setcookie( 'lang', $lang, time() + 60*60*24*30 ); } elseif( !isset($_COOKIE['lang']) ) { $lang = 'en'; } else { $lang = $_COOKIE['lang']; }
Once I get this working, I intend to use a cookie value to display an English or Russian menu using some conditional PHP.
Thanks.
source share