I am trying to store nested arrays in a cookie. I decided to store the array as a JSON string. However, I am getting this warning:
PHP Warning: Cookie values can not contain any of the following ',; \t\r\n\013\014' in foobar.php
Is there a recommended way of storing nested arrays in a cookie?
toString() method is used to convert: an array of numbers, strings, mixed arrays, arrays of objects, and nested arrays into strings.
Cookies can only store string values. You cannot store an array directly into a cookie.
Nested ArraysArray elements can contain anything, including other arrays. You can create arrays with arrays inside them and even arrays inside those inner arrays.
You could use base64_encode()
and base64_decode()
Note that according to the manual:
Base64-encoded data takes about 33% more space than the original data.
If you have some other form of persistence available (db, sessions, memcache), I'd recommend storing the real data there. Then put a unique identifier in the cookie, which can be used to look up the desired data. It's just a lot cleaner and more secure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With