简体   繁体   中英

How to store items in COOKIES array?

I am trying to save the ids of designers the user recently viewed in a cakephp app. My action view() is similar to below:

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

And the view.ctp is

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

And is displaying the id of the designers I have checked. Now how do I save the ids of the designers I have checked into the 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 !

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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