【问题标题】:CakePHP empty key for Security::cipher()用于 Security::cipher() 的 CakePHP 空密钥
【发布时间】:2014-10-27 07:23:06
【问题描述】:

我使用的是 CakePHP 2.2.3 版。我想以数组格式获取我的 cookie,例如-

Array
(
    [0] => Array
        (
            [username] => test1
        )

    [1] => Array
        (
            [username] => test2
        )

    [2] => Array
        (
            [username] => test3
        )

)

下面的代码我收到错误消息。请检查我的代码。

public function broadcast($username=null) { //test1 or test2 or test3
        $this->layout = '';
        $recent = $this->Cookie->read('recentUser');
        $amount = count($recent);
        $cookieVal = array('username'=>$username);
        $this->Cookie->write('recentUser.' . $amount, $cookieVal);
        $test = $this->Cookie->read('recentUser'); 
        pr($test);
        exit;
    }

输出:

Warning (512): You cannot use an empty key for Security::cipher() [CORE\Cake\Utility\Security.php, line 186]

Array
(
    [0] => Array
        (
            [username] => test3
        )

)

我总是得到我上次访问的最后一个 cookie,出现 Secrity::chiper() 错误。请帮助我如何以上述格式显示多个 cookie?

【问题讨论】:

  • 要么你没有使用 2.2.3,要么行号错误,186 行将是return $out。你的Security.salt 配置值是什么样的?
  • @ndm Secuity.salt 问题已解决。 cookie write 应采用 4 参数 name, value, 'false, expire time` 以上代码有效,但我的 cookie 被覆盖。如何在单个变量中设置数组 cookie 值?

标签: cakephp


【解决方案1】:

你可以试试这个看看这是否能解决你的问题

public function broadcast($username=null) { //test1 or test2 or test3
    $this->layout = '';
    $recent = $this->Cookie->read('recentUser');
    $amount = count($recent);
    $amount++; //Custom
    $cookieVal = array('username'=>$username);
    $this->Cookie->write('recentUser.' . $amount, $cookieVal);
    $test = $this->Cookie->read('recentUser'); 
    pr($test);
    exit;
}

【讨论】:

  • 请不要只发布代码,解释您的更改的作用以及它们如何解决问题。如果您无法解释,请不要将其发布为答案,而是使用 cmets 代替。
  • 当他读取数量时,他可能总是读取 0 并覆盖 cookie 处的索引。所以我添加了ammount++来增加到下一个索引。
  • 你好@MponosGeorge 你的答案是同样的错误。我也得到了最后的饼干。 Cookie 覆盖。请检查您的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
相关资源
最近更新 更多