简体   繁体   English

如何在COOKIES数组中存储项目?

[英]How to store items in COOKIES array?

I am trying to save the ids of designers the user recently viewed in a cakephp app. 我正在尝试保存用户最近在cakephp应用程序中查看的设计人员的ID。 My action view() is similar to below: 我的动作view()类似于下面的内容:

$this->Cookie->write('designers', $id);
$cookies = $this->Cookie->read('designers'); 
$this->set("designer_id", $cookies);

And the view.ctp is view.ctp

<h2>COOKIE</h2>
<p>Designer ID from cookie: 
    <?php
        echo $designer_id;
    ?>
</p>

And is displaying the id of the designers I have checked. 并显示我检查过的设计师的id Now how do I save the ids of the designers I have checked into the cookie ? 现在我如何将我检查过的设计师的ID保存到cookie中? I have tried as below: 我试过如下:

$myarray = array();
$myarray[] = $id;
$this->Cookie->write('designers', $myarray);

But the array contains only the id I am currently viewing ! 但该数组仅包含我目前正在查看的id

try this: 尝试这个:

$this->Cookie->write('designers', serialize($myarray));

and this: 和这个:

$cookies = unserialize($this->Cookie->read('designers')); 

Exact, because you are erasing it every time. 确切,因为你每次都要删除它。

You need to get back the cookie to rewrite it : 您需要取回cookie来重写它:

$myarray = $this->Cookie->read('designers');
$myarray[] = $id;
$this->Cookie->write('designers', $myarray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM