【发布时间】:2012-12-14 00:47:26
【问题描述】:
我发布到的页面具有以下代码,并且正确地回显了 cookie:
/* verify.php */
if ($age >= "21" && $location == "USA" && $cookie == "Y") {
$value = "1";
setcookie("age_verified", $value, time()+60*60*24*30);
header("Location: ../portal.php?cookieset");
}
elseif ($age >= "21" && $location == "USA") {
session_start();
$_SESSION['age_verified'] = "1";
header("Location: ../portal.php?sessionset");
}
在 portal.php 上,我无法回显 cookie,但如果选择该选项,会话会显示正常。
/* portal.php */
session_start();
echo $_SESSION["age_verified"];
结果为“1”
/* portal.php */
echo $_COOKIE["age_verified"];
没有结果
我正在尝试实现类似下面的代码块,但它无法正常工作,因为 cookie 没有回显结果 /* 门户网站.php */ session_start();
if($_SESSION['age_verified']!="1"){
header("Location: index.php?no_session");
}
elseif ($_COOKIE['age_verified']!="1"){
header("Location: index.php?no_cookie");
}
else{
echo "";
}
我错过了什么?
【问题讨论】:
-
这不是您遇到的问题,但不要在
Location:标头中使用相对路径。虽然大多数浏览器都可以使用它们,但它们是不允许的。
标签: php cookies session-cookies setcookie