Storing data in cookies...

[b][red]This message was edited by porksoda2K2 at 2003-3-20 9:11:15[/red][/b][hr]

I'm currently trying to store an ARRAY into a cookie...I heard un/serialize() and urlencode/decode can do that. Here's my code:


$an_array = array("WHAT EVER", "THIS", "ARRAY", "CONTAINS");

setcookie('fatcookie', urlencode(serialize($an_array));

//Refresh the page...

$the_array = unserialize(urldecode($_COOKIE['fatcookie']));
var_dump ($the_array);

Now $the_array contains only a string with garbage in it, like

Isn't this suppose to retrieve the original array from the cookie?




BUT on the other hand this DOES work:


$test = array("value1", "value2");
$sertest = urlencode(serialize($test));
$u = unserialize(urldecode($sertest));
var_dump ($u);

This does output a real array structure:

array(2) { [0]=> string(4) "value1" [1]=> string(4) "value2" }



So I guess it's a cookie problem.

I read serialize() transforms mostly anything into a string that can be stored anywhere and reconstructed as the orginal input using unserialize() Now, In my case, I seem to be doing something wrong

Thanks for any kind of help.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!