简体   繁体   中英

Storing large amounts of info within a cookie

I'm working on a demo tool (PHP, jQuery, XHTML), so far so well, except that I have an issue, I need to save certain information temporarily and I'm doing it through cookies, however the cookies' limit in Apache is 4Kb and I have no longer space within the cookie, so I'm wondering how can I keep saving inside the cookie without a problem if I still don't want to send any information to databases nor text files.

I don't know if maybe by using path or other domain I might be able to work things out.

I would really appreciate any help you can provide me :).

Sessions are like Cookies but they just give the client a unique ID ("session ID") and keep the rest of the data on the server.

Of course this is stored within a database or file but that's totally transparent to you, there's no messing about with SQL queries or file reads or anything.

You just need to replace all $_COOKIE with $_SESSION and put session_start(); at the top of your code: http://www.tizag.com/phpT/phpsessions.php

One downside though: PHP sets all session cookies with no timeout, which the browser usually treats as "delete this cookie whenever the browser is closed". See this question for workarounds: How do I expire a PHP session after 30 minutes?

First you should consider if saving that much data in a cookie is really needed. Maybe you can compress your information or just dont need all of it?

The reason is: the cookie is send at every request to the server (this might more then 1). If you serve images from the same domain, you may get over 20 requests each is sending this large cookie. Assuming your cookie holds 5kb of data, you have 100kb just to loop your information through.

see: http://developer.yahoo.com/performance/rules.html#cookie_size

if you need your information just for the current session, why not saving it into a session var (or memcache etc.pp.)? Maybe its okay if you just save an id in the cookie and if nothing to this id is in your session, you load it from database and save it in the session. so you have a one-time access per session.

Maybe its better if you provide some more background information.

You could create multiple cookies, but it's a bad idea. The cookies will go across the wire with every request. Consider putting your session information in a database or cache tier.

I guess you could store non-sensitive information with a DOM element. If you are using jQuery you can use .data() - http://api.jquery.com/data/

However, after the page does a full reload its gone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM