Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing nested arrays in a cookie

Tags:

php

cookies

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?

like image 425
morpheous Avatar asked Apr 22 '10 15:04

morpheous


People also ask

Which method is used to convert nested arrays to objects?

toString() method is used to convert: an array of numbers, strings, mixed arrays, arrays of objects, and nested arrays into strings.

Can you store an array in a cookie?

Cookies can only store string values. You cannot store an array directly into a cookie.

Can you have nested arrays?

Nested ArraysArray elements can contain anything, including other arrays. You can create arrays with arrays inside them and even arrays inside those inner arrays.


2 Answers

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.

like image 188
Tom Haigh Avatar answered Oct 25 '22 18:10

Tom Haigh


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.

like image 34
grossvogel Avatar answered Oct 25 '22 17:10

grossvogel