无法输出PHP cookie的值

时间:2013-02-13 04:12:24

标签: php cookies output

我有一系列Cookie:访问[userid],访问[seriesid],访问[token]

到目前为止,我已经为每个人分配了价值,我没有遇到任何问题。我的问题是当我试图检索每个cookie的值时。

例如,我已经为cookie访问[userid]赋予了如下值:

$userid = "matt";
setcookie("access[userid]", $userid, time()+(300));

稍后我尝试在网页正文中输出此cookie的值:

echo "You are already logged in as $_COOKIE['access[userid]'].";

我尝试了其他几种方法,但没有一种方法有效:

$username = $_COOKIE["access[userid]"];
echo "You are already logged in as $username.";

我的语法有问题吗?

1 个答案:

答案 0 :(得分:1)

我会避免在你的cookie名称中使用方括号,你用数组表示法定义一个cookie变量,试试

echo "You are already logged in as {$_COOKIE['access']['userid']}";

或使用

setcookie("access_userid", $userid, time()+(300));
echo "You are already logged in as {$_COOKIE['access_userid']}";

此行为与表单名称属性与数组结构相同

http://www.php.net/manual/en/faq.html.php#faq.html.arrays