Rafael Sedrakyan
Rafael Sedrakyan

Reputation: 2629

Use COOKIES to redirect user with PHP

I have a web page which consists of two parts: english and armenian. Each part is placed in one folder. Armenian part in /arm and english part in /eng. I need my index.php file in main directory to define if the user last been in armenian part or english and redirect him/her to that part. For that reason I have placed this code at the top of each php file in both parts:

$expire = time()+60*60*24*7;
setcookie("lang", $language, $expire);

and when user types www.mysite.mydomain, index.php file in my root directory must check the cookies and redirect the user to the arm/index.php or eng/index.php. Here is the code:

if (isset($_COOKIE['lang']))
{
    header("Location: " . $_COOKIE['lang'] . "/index.php");
}
else
{
    header("Location: eng/index.php");
}

The problem is that the user is being redirected only to eng/index.php. Please help me with this problem.

Upvotes: 0

Views: 285

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318468

$_COOKIE is not updated until the next request (when the browser sends back the cookie).

Simply set it on your own after setting the cookie:

$_COOKIE['lang'] = $language;lang", $language, $expire);

Upvotes: 1

Related Questions