CakePHP将多个值设置为一个cookie

CakePHP将多个值设置为一个cookie,php,cakephp,cookies,Php,Cakephp,Cookies,我知道可以通过以下方式将多个值设置为cookie: If you want to write more than one value to the cookie at a time, you can pass an array: $this->Cookie->write('User', array('name' => 'Larry', 'role' => 'Lead') ); 由于一些设计问题,我需要在控制器操作的不同部分设置cookie值。但这种简化

我知道可以通过以下方式将多个值设置为cookie:

If you want to write more than one value to the cookie at a time,
    you can pass an array:

$this->Cookie->write('User',
    array('name' => 'Larry', 'role' => 'Lead')
);
由于一些设计问题,我需要在控制器操作的不同部分设置cookie值。但这种简化的代码似乎不起作用:

public function myfunction() {
    $text = "";
    // to be sure that this cookie doesn't exist
    $this->Cookie->delete('mycookie');
    // getting data from cookie, result is NULL
    $data = $this->Cookie->read('mycookie');
    $text .= "data 1 type: ".gettype($data)."<br>";
    $key="mike";
    $value=12;
    // adding key-value to cookie
    $data[$key] = $value;
    // serializing and writing cookie
    $dataS = json_encode($data);
    $this->Cookie->write('mycookie', $dataS, FALSE, '10 days');

    // reading cookie again, but this time result is
    // string {"mike":12} not an array
    $data = $this->Cookie->read('mycookie');
    $text .= "data 2 type: ".gettype($data)."<br>";
    $key="john";
    $value=20;
    // Illegal string offset error for the line below
    $data[$key] = $value;
    $dataS = json_encode($data);
    $this->Cookie->write('mycookie', $dataS, FALSE, '10 days');

    echo $text;

}
根据上面的代码,“Mike 12”成功设置为cookie。但是当我第二次读取cookie数据时,我得到了如下字符串:
{“mike”:12}
。不是数组

当我为“数据2”生成
gettype
时,输出为“字符串”。
因此,当我将
$data[“john”]=20时,我会得到
非法字符串偏移错误
,因为
$data
不是字符串数组

那么,不可能在一个操作中逐个设置相同的cookie吗

编辑:
当我创建一个数据数组时,json_对该数组进行编码并将其内容写入cookie。然后,在另一个控制器中,当我读取cookie内容并将其分配为变量时,它会自动转换为数组。

以JSON格式对数据进行编码和解码是Coookie组件的内部功能,应用程序不应依赖它!实现可能会更改,代码将中断

目前,JSON数据的解码仅在实际从cookie读取数据时发生,这需要一个新的请求。在同一个请求中,您将访问组件缓冲的原始数据

因此,与此JSON内容不同,您应该遵循约定并传递一个数组:

$data = array();

$key = 'mike';
$value = 12;

$data[$key] = $value;

$this->Cookie->write('mycookie', $data);

// ... do some stuff, and then somewhere else:

$data = $this->Cookie->read('mycookie');

$key = 'john';
$value = 20;

$data[$key] = $value;
$this->Cookie->write('mycookie', $data);
或使用点符号(这将导致多个cookie):


另请参见

如果存储的是(JSON)字符串,为什么希望组件返回不同的内容?因为在正常情况下,Cookie中JSON_编码的字符串返回为数组。当我解码它时,我得到一个错误。错误是“它已经是数组而不是字符串”,这只会在实际从cookie读取数据时发生。如果没有新的请求,它将读取存储在组件缓冲区中的数据,该缓冲区按原样保存数据。我不知道存在组件缓冲区。我没有在我读到的文件中看到……谢谢你的回答。我理解我的错误。。但是你提到蛋糕的内部功能可以改变。那么,我应该做些什么来保证未来版本的安全呢?在编写cookie之前,我对数组进行编码。当我读取cookie时,我只使用CookieComponent的read函数,而不进行json_解码。因为结果以数组的形式出现。我应该再次解码输出吗?谢谢。如示例所示,根本不要编码为JSON,而是传递原始数组,组件将处理它。或者,在适当的情况下,使用点符号样式。这两种方法都可以安全使用,并且在主要版本更改(如2.x到3.x)之前不会中断。我对数据进行编码,因为有时我会向cookie添加重要字符串,它们可以包含“;”字符。这不是问题,cookie最终是使用URL编码格式发送值的方式设置的,所以像
可以安全使用,即使不使用cookie组件的加密功能。
$data = array();

$key = 'mike';
$value = 12;

$data[$key] = $value;

$this->Cookie->write('mycookie', $data);

// ... do some stuff, and then somewhere else:

$data = $this->Cookie->read('mycookie');

$key = 'john';
$value = 20;

$data[$key] = $value;
$this->Cookie->write('mycookie', $data);
$key = 'mike';
$value = 12;
$this->Cookie->write('mycookie.' . $key, $value);

// ... do some stuff, and then somewhere else:

$key = 'john';
$value = 20;
$this->Cookie->write('mycookie.' . $key, $value);