【问题标题】:CakePHP setting multiple values into one cookieCakePHP 将多个值设置到一个 cookie 中
【发布时间】:2013-11-13 05:45:17
【问题描述】:

我知道可以通过这种方式为 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;

}

页面输出:

Warning (2): Illegal string offset 'john' [APP/Controller/MyController.php, line 2320]  
data 1 type: NULL   
data 2 type: string  

从上面的代码中,“Mike 12”成功设置为cookie。但是当我第二次读取 cookie 数据时,我得到一个像这样的字符串:{"mike":12}。不是数组。

当我为“数据 2”创建gettype 时,输出为“字符串”。
所以当我创建$data["john"]=20 时,我得到Illegal string offset error 因为$data 是字符串而不是数组。

那么是不是不能在一个动作中一个一个地设置相同的cookie?

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

【问题讨论】:

  • 您正在存储一个 (JSON) 字符串,为什么您希望组件返回不同的内容?
  • 因为在正常情况下,cookies 中的 json_encoded 字符串会以数组形式返回。 json_decode 时出现错误。错误是“它已经是数组而不是字符串”
  • 只有在实际从 cookie 中读取数据时才会发生这种情况。如果没有新请求,它将读取存储在组件缓冲区中的数据,该缓冲区按原样保存数据。
  • 我不知道存在组件缓冲区。我没有在我阅读的文件中遇到..

标签: php cakephp cookies


【解决方案1】:

以 JSON 格式编码和解码数据是 Coookie 组件的内部功能,您的应用程序不应依赖它!实现可能会发生变化,您的代码会中断。

目前,只有在实际从 cookie 中读取数据时才会对 JSON 数据进行解码,这需要新的请求。在同一个请求中,您将访问组件缓冲的原始数据。

因此,您应该遵循约定并传递一个数组,而不是这个 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):

$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);

另见http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#using-the-component

【讨论】:

  • 感谢您的回答。我理解我的错误.. 但是你提到 Cake 的内部功能可以改变。那么我应该怎么做才能保证未来版本的安全呢?在编写 cookie 之前,我对数组进行了编码。当我读取 cookie 时,我只使用 CookieComponent 的读取函数,而不使用 json_decode 它。因为结果以数组的形式出现。我应该再次对输出进行 json_decode 吗?谢谢。
  • 如示例中所示,根本不编码为 JSON,而是传递原始数组,组件将处理它。或者,在适当的情况下,使用点符号样式。这两种方法都可以安全使用,并且预计在主要版本更改(如 2.x 到 3.x)之前不会中断。
  • 好吧,我对数据进行编码是因为有时我会在 cookie 中添加重要的字符串并且它们可以包含“;”字符。
  • 这不是问题,cookie 最终是使用setcookie() 设置的,它以 URL 编码格式发送值,所以像; 这样的字符可以安全使用,即使不使用加密功能cookie 组件。
猜你喜欢
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
  • 2012-12-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
  • 2013-07-08
  • 2012-10-14
相关资源
最近更新 更多