How to store large JSON String in cookie ? I have to use cookie only, session is not an option in my case. Can anyone post example code to compress string and store in cookie and also retrieve that successfully. Thanks.
-
Did you read http://php.net/manual/en/features.cookies.php? – Jon Mar 27 '12 at 11:30
-
Have a look at http://stackoverflow.com/questions/4225030/jquery-save-json-data-object-in-cookie – Naveed Mar 27 '12 at 11:32
-
@NAVEED: No issue to store small json string in cookie. – Riz Mar 27 '12 at 11:35
3 Answers
Compressing the data doesn't seem like such a great idea. Rather, I'd keep it in a database and only store the database entry's ID in a cookie. That would also prevent people from tampering with the data, albeit tampering with the ID will still be possible. Using sessions would be better and eliminate this.
However, if you insist on storing the data in a cookie, you can compress the data using either gzcompress()
, gzdeflate()
or gzencode()
. These all offer compression. gzdeflate()
would be the best choice for your problem, seeing that it is most space-efficient.
$compressedJSON = gzdeflate($json, 9);
setcookie('json', $compressedJSON);
And to read it
$compressedJSON = $_COOKIE['json'];
$json = gzinflate($compressedJSON);
Keep in mind that even if the compression will be enough for your data to stay within the 4K limit, you might eventually exceed that, should the amount of JSON data you need stored grow.
I still suggest you use a database instead.

- 19,333
- 5
- 62
- 89
-
Note that to use gz* function zlib extension must enabled, be default it is not (on non-windows machines). – Jevgenij Evll Mar 27 '12 at 12:04
-
Something like this?
Encode
$sJSON = json_encode($sSomeJSONData);
setcookie('json', $sJSON);
Decode
$sJSON = json_decode($_COOKIE['json']);
--- EDIT ----
Go with Kristian's answer for using gzip or a database it makes the most sense... BUT if you can't use a database you could build the session manually.
All a session is, technically, is a text file stored outside the web-tree containing data. You could duplicate that process using something like uniqid()
to generate a "session name".
Create a text file, in a given directory, using that "session name" as the file name and store the "session name" in your cookie.
Then simply use serialize()
, unserialize()
and file_put_contents()
, file_get_contents()
to transfer your data between your program and your text file, using the data in the cookie to find the text file.
It would need some sanity checks and hijacking prevention but the principle is sound.

- 8,332
- 3
- 24
- 28
-
-
There is a 4K limit for the entire cookie, including name, value, expiry date etc. I think what @dev asks is how to store a data in the cookie if the data exceeeds that limit. – ahmetunal Mar 27 '12 at 11:42
-
@marvin: great! you are almost there. I was looking for solution to compress data and store in a cookie. But if someone can post similar (e.g; using multiple cookies) to that, I shall be thankful. – Riz Mar 27 '12 at 11:49
-
-
@marvin : ahh, looks like I may have slightly misunderstood the question then, a 4k cookie? That's nuts... go with Kristian's answer using gzip (or better yet, as he suggests) a database. – CD001 Mar 27 '12 at 12:08
If your information is that large, you may want to look into using local storage instead. Not all browsers support local storage, but all modern ones do. If your cookie is too large, you run the risk of getting 431 http error.
So you don't have size issues, and don't have to pass a large cookie with every request, you should just store a unique ID in the cookie. Then you can retrieve the large data object from memcache, mysql, or any other server side storage using the unique ID. No sessions required.

- 12,641
- 2
- 26
- 39
-
would you tell me how large coolie can we store.... can we store the cookie like 500 words ? – StaticVariable Aug 21 '12 at 03:05
-
A cookie can be up to 4K. That's the entire cookie (metadata + data). If you are getting anywhere close to that, and need to store items locally, you should use local storage. All modern browsers support it. – Brent Baisley Aug 27 '12 at 15:04