I'm using the following functions to set an array of values in a cookie in PHP, but I also need an "add" and "edit" function - any suggestions on how I can do that?
function build_cookie($var_array) {
if (is_array($var_array)) {
foreach ($var_array as $index => $data) {
$out.= ($data!="") ? $index."=".$data."|" : "";
}
}
return rtrim($out,"|");
}
function break_cookie ($cookie_string) {
$array=explode("|",$cookie_string);
foreach ($array as $i=>$stuff) {
$stuff=explode("=",$stuff);
$array[$stuff[0]]=$stuff[1];
unset($array[$i]);
}
return $array;
}
Usage:
setcookie("mycookies", build_cookie($cookies_array), time()+60*60*24*30);
$cookies_array2 = break_cookie(urldecode($_COOKIE['mycookies']));
foreach ($cookies_array2 as $k => $v) {
echo "$k : $v <br />\n";
}