I'm trying to set up, what I thought would be, a simple language switch. I thought I'd use PHP cookies, but they're not behaving as intended.
I've read a few cookie tutorials and looked at a few similar examples here on StackOverflow, but I must be missing something because it can't get it to work properly.
I'm setting the language by passing it as a URL variable (lang=en or lang=ru). That all seems to be fine. However, the code I have at the moment that sets the cookie seems to be one step behind, so initially it has no value (I'd like it to be 'en' by default), then if the user clicks the 'ENG' button it still has no value, and then if the user clicks Russian the value shows as 'en', and then if I click the 'ENG' button again the value shows as 'ru'.
Here's the code I've cobbled together:
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've got this working I intend to use the value of the cookie to display either the English or Russian menu using a bit of conditional PHP.
Thanks.