I'm trying to get a simple language initialisation working, below are the clauses. Have a missed any possible situations where a user wouldn't have either a default language set or a selected language set?!
lang
is POSTed the assign it to the $_SESSIONlang
is not POSTed the see if $_SESSION doesn't exist and assign defaultOtherwise do nothing as $_SESSION is already set with selected language and populated.
if(isset($_POST['lang'])) {
$_SESSION['lang'] = $_POST['lang'];
} else {
if(!isset($_SESSION['lang'])) {
$_SESSION['lang'] = 'en_uk';
}
}
It looks fine if you want the lang in the session. A cookie would be better though.
You also might want to have some kind of validation on the POSTed value before you save it to the session or go look for the strings in the database or files.
if(!preg_match('/^[a-z]{2}_[a-z]{2}$/', $_POST['lang']))
die('Wrong lang parameter format');
Putting language in the session can have strange unintended effects.
Sessions are .. well .. session wide. If I open two windows to your website, and I change the language on one, the language on the other will switch too when I load another page.
Put the language in the URL, like:
This effect becomes even stranger when the 'next page' is not a page, but, for instance a form that I'm submitting.