简体   繁体   English

使用COOKIES使用PHP重定向用户

[英]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 . 亚美尼亚语部分在/arm而英语部分则在/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. 我需要主目录中的index.php文件来定义用户上次使用亚美尼亚语还是英语并将其重定向到该部分。 For that reason I have placed this code at the top of each php file in both parts: 因此,我将这段代码放在两个部分的每个php文件的顶部:

$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 . 当用户键入www.mysite.mydomain ,我根目录中的index.php文件必须检查cookie并将用户重定向到arm/index.phpeng/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 . 问题在于用户仅被重定向到eng/index.php Please help me with this problem. 请帮我解决这个问题。

$_COOKIE is not updated until the next request (when the browser sends back the cookie). 直到下一个请求(浏览器发回cookie时), $_COOKIE COOKIE才会更新。

Simply set it on your own after setting the cookie: 设置cookie后,只需自行设置即可:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM