Header Location with page refresh

I am using the following statement for a language switcher (dropdown):

if ( !$lang ) {
    if ( isset($_COOKIE["language"]) ) {
        $lang = $_COOKIE["language"];
    } else {
        $lang = 'nl';
    header("Location: /$lang/$route");
} else {
    $lang = (string)$lang; 
    setcookie( 'language', $lang, time() + 60*60*24*365, '/' );	

Basically this works fine. When I choose a language from the dropdown (language flagg and name) the Cookie language gets a new value but at the same time the choosen language (flagg and name) should become tha active link as well. For this to happen I use the following if else statement.

<li class="nav-item dropdown">
    <?php if (!isset($_COOKIE["language"]) || isset($_COOKIE["language"]) && $_COOKIE["language"] == 'nl' ): ?>
    <a class="nav-link dropdown-toggle" href="#">
		<img src="/images/flaggs/nl.png">&nbsp;&nbsp;Nederlands
		<div class="dropdown-menu dropdown-menu-right mt-0">
			<a href="/en<?php echo $current_route?>" class="dropdown-item">
				<img src="/images/flaggs/uk.png">&nbsp;&nbsp;English
	<?php elseif (isset($_COOKIE["language"]) && $_COOKIE["language"] == 'en' ): ?>
    <a class="nav-link dropdown-toggle" href="#">
        <img src="/images/flaggs/uk.png">&nbsp;&nbsp;English
        <div class="dropdown-menu dropdown-menu-right mt-0">
			<a href="/nl<?php echo $current_route?>" class="dropdown-item">
                <img src="/images/flaggs/uk.png">&nbsp;&nbsp;Nederlands
    <?php endif; ?>

With just the:

header("Location: /$lang/$route");

this isn’t happening. Right now I need to manually refresh the page befor the value changes. What do I need to adjust to change the active language? Thank you in advance

The value in the cookie will only become available in $_COOKIE the request after it was set. You should use another way to get to value to the template the first time, or always redirect after the cookie was set.

Also, how are you calling the template?

@rpkamp. The template is called through the normal MVC structure:

  • Person visits the website for the first time ($_COOKIE[‘language’]) doesn’t exist so it is set with the default nl value (www.website.com/nl/)
  • Person choose English from the dropdown ( <a href=“/en<?php echo $current_route?>”) and the English version will be presented

So bassically the person will stay on the same page/template only the language will change , where /en in the anchor is $lang and $current_route the $route:

header("Location:  /$lang/$route");

I hope i made myself clear

In that case your template should just be passed a variable, it shouldn’t know anything about cookies.

Now I’m loosing you. What do you mean. Sorry fo my ignorance :face_with_raised_eyebrow:

Your template code refers to $_COOKIE. But it’s a template, it shouldn’t know anything about cookies. The controller should pass the value of the current language to the template, and if that value come from a cookie than that’s fine, but it’s an implementation detail of the controller.

So in the controller you pass a variable $language to the template, and in the template you just use that value, not $_COOKIE.

@rpkamp. It’s not realy the cobtroller where I define the $_Cookie, it’s in the file where all the PATHS are defined, which is included in index.php. I could add it to the main Controller, but I have no idea how to do so. Can you please give me a simple example on how I should approach this.

Thank you in advance

Reading this again, why do you need a cookie when the current language is also in the URL? :thinking:

@rpkamp. Because I want the language to be remembered for a future visit Otherwise they have to change the language again. I know it is not a lot of work, but see it as bit of a service

And how do you expect google to crawl the multiple languages of your website?

Hint: don’t mix language selection and cookies, it becomes one giant mess. People should go to the domain of the language they want.

@rpkamp. I hear what you say and actually completely agree only sometimes you have to take specific wishes from a customer into account.

Okay so what I would do is use the cookies to redirect to a different domain if the cookie value doesn’t match the current URL. In the controller and template, just use the URL to see which language should be used, and use that. Don’t use the cookie in controller and/or template, they don’t belong there.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.