Storing data in cookies...

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

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:

[code]

<?php
$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
%3AWHAT%34EVER%4%ATHIS%AARRAY%3A%CONTAINS%3B

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

Thanks.

*/
?>

[/code]

BUT on the other hand this DOES work:

[code]

$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" }

*/

[/code]

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!

Categories