CodeIgniter Forums
Array cookie - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Array cookie (/thread-11341.html)



Array cookie - El Forum - 09-04-2008

[eluser]EEssam[/eluser]
From PHP manual:

Example #3 setcookie() and arrays

You may also set array cookies by using array notation in the cookie name. This has the effect of setting as many cookies as you have array elements, but when the cookie is received by your script, the values are all placed in an array with the cookie's name:

<?php
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");

// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {
echo "$name : $value <br />\n";
}
}
?&gt;

How this can be done with CI cookie's library?
http://ellislab.com/codeigniter/user-guide/helpers/cookie_helper.html

Thanks.


Array cookie - El Forum - 09-04-2008

[eluser]fatnic[/eluser]
Not sure about CodeIgniter allowing it, but in the mean time you can serialize your data into one string so you can store the array in a single cookie.

Just one option.


Array cookie - El Forum - 09-04-2008

[eluser]ontguy[/eluser]
From looking at the helper it looks like it does support arrays.

Seems like it may work something like this:
Code:
set_cookie(array('name' => 'cname', 'value' => 'cvalue1'));
set_cookie(array('name' => 'cname', 'value' => 'cvalue2'));



Array cookie - El Forum - 09-04-2008

[eluser]fatnic[/eluser]
The second set_cookie call just overwrites the first one.

EDIT:
I just gave this a try...
Code:
set_cookie(array('name' => 'cname[one]', 'value' => 'cvalue1', 'expires' => '86500'));
set_cookie(array('name' => 'cname[two]', 'value' => 'cvalue2', 'expires' => '86500'));
        
print_r(get_cookie('cname'));

And it worked fine. Shouldn't be a problem using a cookie array.


Array cookie - El Forum - 09-05-2008

[eluser]Bramme[/eluser]
What's wrong with:

Code:
$array['one'] = 'value';
$array['two'] = 'value2';
set_cookie(array('name'=>'array', 'value'=$array));

print_r(get_cookie('array'));
??


Array cookie - El Forum - 09-05-2008

[eluser]fatnic[/eluser]
Because I'm pretty sure you can't store an array in a cookie.


Array cookie - El Forum - 09-05-2008

[eluser]Bramme[/eluser]
you can't? Oh...

I thought I'd used it once, but now you mention it, I don't think I ever did...