2

我有一组 cookie:access[userid]、access[seriesid]、access[token]

到目前为止,我已经为它们中的每一个分配了值,这样做没有问题。我的问题是当我试图检索每个这些 cookie 的值时。

例如,我为 cookie access[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.";

我的语法有问题吗?

4

1 回答 1

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

于 2013-02-13T04:24:20.553 回答