Currently creating a small website, i'm trying to add a multi langage tool between french and english. But i'm facing some problems with it. Basically, I have two buttons, english and french to change langage. But if it's first visitor time, a default langage should be set (here, french).
So i created a small script, that doesnt work as i expect...
if(!isset($_COOKIE['lang_ylx'])) {
$timestamp_expiration = time()+30*24*3600 ;
if(!isset($_GET['lang'])) {
setcookie('lang_ylx', 'fr',$timestamp_expiration, null, null, false, true);
}
else if ($_GET['lang']=='fr') { // si la langue est 'fr' par le bouton de langue, on définit le cookie francais
setcookie('lang_ylx', 'fr',$timestamp_expiration, null, null, false, true); //On définit un cookie de langue
header('Location: index.php');
}
else if ($_GET['lang']=='en') {
setcookie('lang_ylx', 'en',$timestamp_expiration, null, null, false, true);
header('Location: index.php');
}
That script is automatically loaded withind index.php with include. For now, I only have index.php and i'm trying, when I click for example english button, to pass a parameter in URL, then alter the cookie so it gets 'en' value, and then go back to index.php.
My lang files are two giant loop that test the value of the cookie. But whenever I click english or french button, I'm stuck at lang.php, but it should go back to index.php, with new langage.
Any Idea ?
$timestamp_expiration = time()+30*24*3600 ;
if(!isset($_COOKIE['lang_ylx'])) {
//If you want your default to be english, use this:
setcookie('lang_ylx', 'en', $timestamp_expiration, null, null, false, true);
//French:
setcookie('lang_ylx', 'fr',$timestamp_expiration, null, null, false, true);
}
/*if(!isset($_GET['lang'])) {
setcookie('lang_ylx', 'fr',$timestamp_expiration, null, null, false, true);
}*/
//Comment out above because you're setting french every time llang isn't set.
if (isset($_GET['lang']){
else if ($_GET['lang']=='fr') { // si la langue est 'fr' par le bouton de langue, on définit le cookie francais
setcookie("lang_ylx", "", time()-3600);
setcookie('lang_ylx', 'fr',$timestamp_expiration, null, null, false, true); //On définit un cookie de langue
header('Location: index.php');
}
else if ($_GET['lang']=='en') {
setcookie("lang_ylx", "", time()-3600);
setcookie('lang_ylx', 'en',$timestamp_expiration, null, null, false, true);
header('Location: index.php');
}
}
You had: