cookies and arrays in php
How to store and retreive arrays using cookies with php:
The first step is to turn the array into a string with each array value seperated by a delimiter. I usually choose an unusual delimiter character such as a vertical bar, as the array values are more likely to contain characters such as comma or slashes. Example:
Read on…










Blend your own recipe….
$a = array(’bg’=>’red’,'tx’=>’blue’,'lk’=>’green’);
print_r($a);
$b = implode(”|”,array(’red’,'blue’,'green’));
print_r($b);
$c = explode(”|”,$b);
print_r($c);
this is an expensive solution. PHP cookies, itself supports storing arrays check this link http://www.developertutorials.com/tutorials/php/articlename-050526/page5.html
That is soooo inefficient. Use the serialize() function thus:
setcookie(”myarray”, serialize($myArray), time()+3600);
and to get data out:
if(isset($_COOKIE['myarray'])) $myArray=unserialize($_COOKIE['myarray']);
So, what do You think about sth like this:
<?php
if(isset($_COOKIE['c'])) {
$a = unserialize($_COOKIE['c']);
$a[] = time();
setcookie(’c', serialize($a), time() + 1000);
} else {
setcookie(’c', serialize(array(time())), time() + 1000);
}