Define cookies and then refresh the page, PHP


I'm sure this topic had been addressed before, but I can't seem to find an adequate solution to my problem, which I'm sure is not unique.

So I get that you can't set a cookie and expect to use without refreshing the page. So I'm wondering what my options are.

I have a simple set of links that change the language on the page by setting a cookie to that user's language preference. I need to detect that cookie to assign a variable so that I can then change the page output to the designated language.

So, when the button is pressed, it sends a get variable to the URL bar, which then sets the cookie. After I refresh the page, I get what I want.

Basically, I need to pass the GET variable and then refresh the page. How can I do this?

my php code:

// if someone is trying to change the language
    // change the cookie value to that language
    $value = $_GET['lang'];
// elseif they are not trying to change the language, and a cookie is already set
    // maintain the value of the language set in the cookie
    $value = $_COOKIE['language_pref'];
// if get nor cookie is set
    // set default language to english
    $value = 'en_US';
$name = 'language_pref';
// cookie expires in 2 years
$expireDate = time() + (2 * 365 * 24 * 60 * 60);
$path = '/';
$domain = '';
$secure = false; //only transmit the cookie if a HTTPS connection is established
$httponly = true; //make cookie available only for the HTTP protocol (and not for JavaScript)
setcookie( $name, $value, $expireDate, $path, $domain, $secure, $httponly);


<a href="?lang=zh_CN">ZH</a>
<a href="?lang=en_US">EN</a>

Ok so here is what your page looks like:

> Read cookie and get the language
> Read GET variable and SET COOKIE
> Print out stuff in their language

You are simply doing things in the wrong order. If you do things in this order:

> Read GET variable and SET COOKIE
> Read cookie and get the language
> Print out stuff in their language

You'll already have the right language and have no need to refresh the page.