Unable to read php serialized arrays stored in cookies
Posted Saturday, June 28th, 2008, under programmingThis was the problem:
I was storing an array in a cookie using php’s serialize() function then retrieving it by using the unserialize() function. Simple enough, and it was working perfectly on my local development machine.
However, although the string seemed, on the face of it, to be being stored without a problem on the live server, the unserialize() function was not returning an array. In fact, it was not returning anything.
The reason, as ever in these kinds of hair-pulling situations, was a simple one. magic_quotes_gpc was set to on on the live server, meaning that quotes were being escaped, meaning that unserialize() went bang.
The solution:
function RetrieveCookieArray($cookie) {
return get_magic_quotes_gpc() ? unserialize(stripslashes($cookie)) :
unserialize($cookie);
}





