PHP cookies and redirection

Tags: , , , ,



I have a website in two languages: English and French. And I want to redirect people based on their browser languages: if there browser language is French, I redirect people on /fr, else they go to the usual site (i.e. no redirection).

In order to do this, I used at first a PHP session (with the PHP function session_start) and it worked fine. But the problem with session is that you don’t have control over the time of the cookie bound to the session (I read that the session last till you close your browser, but in my case it doesn’t work: the session’s cookie is still there when I close and open again my browser). Thus, I decided to use a cookie (with the PHP function setcookie instead) to have control over its time duration. So I adapted my code previously made for the session (which was working) to use the setcookie function instead:

// If the cookie "first_time" is not defined, we define it 
// and do the redirection if the browser language is French,
// else we do nothing.
// In this case, we only redirect (or not) the first time we go on the website,
// which is exactly what we want.
if( !isset($_COOKIE["first_time"]) ){

    $browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

    setcookie("first_time", "", time()+3600, "/", "", 0);

    if ($browser_lang == "fr") {
        header("Location: /fr");
    }
}

However, I got a Firefox error saying:

The page isn’t redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete. This problem can sometimes be caused by disabling or refusing to accept cookies.

The problem is when I do a redirection with a PHP session, I don’t have this error. Moreover, when I do the below code (which is not doing what I want), I have no problem also (the redirection occurs):

if( !isset($_COOKIE["flag"]) ){

    $browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

    setcookie("flag", "0", time()+30, "/", "", 0);
}
else {
    if ($_COOKIE["flag"] == "0") {
        header("Location: /fr");
        setcookie("flag", "1", time()+30, "/", "", 0);
    }
}

I have two questions:

  1. Why the redirection is not working with my first piece of code, but working with the second piece of code?
  2. Another question not related to this: Why when I set a cookie with setcookie I have to wait a page reload so that I can access the cookie value with $_COOKIE (from PHP manual:”Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE array”), but with session cookie I can access its variables right away with $_SESSION?

Any help will be greatly appreciated.

Answer

Try:

if( !isset($_COOKIE["first_time"]) ){

    $browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

    setcookie("first_time", "1", time()+3600, "/", "", 0);
    if( !isset($_COOKIE["first_time"]) ){   
    header("Reload:0");
    }
    if ($browser_lang == "fr") {
        header("Location: /fr");
    }
}


Source: stackoverflow