1

First of all, I must precise that I'm quite a noob in web development, but I'll do my very best.

I created a multingual website in English and French. Contrary to most people I read on the internet that advise to create a different php page per language, I created a MySql array to have a proper localization of the website. I find this solution much cleaner and more elegant, as I plan to add more languages sooner or later.

I have a Config.php file with the path to MySql. By default, language is English, unless you decide to display another language, like French :

  $_SESSION['lang'] = "en"; 
}
else{
  switch($_GET['lang']){
      case "fr":
      $_SESSION['lang'] = "fr";
      break;
      case "en":
      $_SESSION['lang'] = "en";
      break;
      default :
      $_SESSION['lang'] = "en"; 
      break;
  }
}
switch($_SESSION['lang']){ 
      case "fr":
      $language = "French";
      break;
      case "en":
      $language = "English";
      break;
}

Obviously, Config.php is included in all other php pages. In order to change the language, I added images with flags in the navigation bar, which will add a ?lang= at the end of the url :

<a href="?lang=fr"><img class="language" src="images/fr.png"></a>
<a href="?lang=en"><img class="language" src="images/en.png"></a>```

So, if you choose French, it adds ?lang=fr to the url. Localization works great, I'm happy.

However, when I navigate to a different page from the navigation bar, it loses the French language (as English is the default one) :

    <a href="index.php"><?php echo $loc["header_home"] ?></a>

I understand that it makes sense since the link is only index.php and not index.php?lang=fr.

So, I tried to go for cookies in order to keep the chosen language, and this is where I'm stuck.

I edited my Config.php to set and Cookies like this :

     if ((isset($_GET['lang']) != "")) {
        setcookie("langpref", "".$_GET['lang']  ."", time()+(0), "/", 'domain.com'); //expires at the end of the session//
        $_COOKIE['langpref'] = $_GET['lang'];
    }

No problem so far, until I add these lines :

if (isset($_COOKIE['langpref'])) {
    if (strpos($_SERVER['PHP_SELF'],$_COOKIE['langpref']) === false) {
      header("location: ". basename($_SERVER['PHP_SELF']). "?lang=". $_COOKIE['langpref'] );
    }
}

I assume this is a naive approach to add ?lang= in the url like this. Because when I do, I've got an error message that says the page cannot be loaded because cookies are not allowed or something.

Any help (or different approach) would be welcome !

Thank you in advance !

PandaRasta
  • 321
  • 4
  • 15
ElodieLOD
  • 11
  • 1
  • Likely related: https://stackoverflow.com/q/8028957/1169798 – Sirko Dec 05 '19 at 10:57
  • Passing the info what language is requested via cookie / session is _not_ the proper way to do this in the first place. Search engine bots will likely not care about those, so you will have problems with getting your content indexed properly; As a user, I will not be able to pass on / share the link to particular language version either (because the cookie won’t magically share along as well), and even my own local bookmark might show the “wrong” language, once the cookie has expired. **This info belongs into the URL.** – 04FS Dec 05 '19 at 11:06
  • (You don’t need a different _script_ for that though, or copy the same script file into multiple different “language folders” or anything like that - go read up on _URL rewriting_.) – 04FS Dec 05 '19 at 11:08
  • Assuming you want to continue down this path, you should check that there is no output being sent to the browser before you call the header() function. Output can be a new line character or a space. If you want to use cookies, I suggest using JavaScript to create the cookie. This saves you a round trip to the server. – Syed Hussim Dec 05 '19 at 11:19
  • @04FS : Thanks for the header advice. I changed that, but no avail. Regarding the way to index languages, I disagree with you since you can send the url with the "?lang=" and it will force the page to load with the proper language. – ElodieLOD Dec 05 '19 at 15:15
  • @SidHussain : thanks but unfortunately, I know nothing about javascript :/ – ElodieLOD Dec 05 '19 at 15:16
  • @04FS : Ok, regarding the URL rewriting, I read a few things about it. I understand how it works (shortening an url for instance), but I'm still puzzled on how it can be connected to the chosen language. I probably have to search a little more... :/ – ElodieLOD Dec 05 '19 at 16:57

1 Answers1

0

Written as an answer because not enough space in the comments.

You're setting your cookie to expire immediately, I've changed this to an hour for you.

if ((isset($_GET['lang']) != "")) {
    setcookie("langpref", $_GET['lang'], time()+3600, "/"); //expires at the end of the session//
    $_COOKIE['langpref'] = $_GET['lang'];
}

Other:

You've written "".$_GET['lang'] ."" when you can just write $_GET['lang']

Mark
  • 1,852
  • 3
  • 18
  • 31