1

I have read that you can have 20 cookies per domain, 4kb per file.

I am currently setting cookies like this:

setcookie($cookieName, $cookieData , time()+60*60*24*30, "/");

if name is "One" and data is "111111111"... then i set another cookie "Two" with the data "222222222"... i then have a single file with the below data

One
111111111
192.168.1.2/
1536
673206400
30159100
1505317760
30153065
*
Two
2222222222
192.168.1.2/
1536
983206400
30159100
1820257760
30153065
*

How do i go about creating two different files? I want to pass the restriction of only being able to have a max of 4kb for our intranet site's cookies. All of these cookie files would be from the same site...

3
  • It is hard to follow what you are trying to do, please expand on your question, so we can help you. Commented May 23, 2011 at 17:56
  • It would be very hack-y but you could use a more or less specific subdomain for more cookies (I.E. use both *.domain.com and www.domain.com). I can't make any guarantee that will work, either. Commented May 23, 2011 at 17:56
  • Also, I feel the most important thing to say is the obligatory "don't do it" answer - persist substantial data and you won't have this problem. This is not what cookies were made for. Commented May 23, 2011 at 18:01

1 Answer 1

2

You would usually not want to use cookies for this, but start a session (that uses a cookie to identify the user), and store the information on server side.

Alternatively, if the session lifetime is too short for your purposes, create a cookie with a longer lifetime, and store a random key in it. Use that key to store and look up your data on server side.

If you must store stuff locally, there are more developed client-side storage strategies that can accept more data than that. See

Sign up to request clarification or add additional context in comments.

4 Comments

The reason for this is that when someone selects specific criteria on a page, we want to maintain those selections over a period of days... and storing the data in a database is unfortunately not an option for me... and sessions dont persist after the browser is closed. It seems like Local Storage might be a possibility though; i will review this a bit.
@adam you could either extend the session lifetime, or use the same mechanism but without using sessions: Create a unique ID, store that in a cookie, store stuff server side with that ID (in a database or file), if the browser sends the cookie with the ID, look up the data.
@adam just so you know sessions have nothing more to do with browsers than cookies do. A session is key-based storage on your server. A cookie in the browser that is set to expire on close maintains the key to the right session. So it's theoretically possible to retain a session by storing the session key cookie in a different, longer-life cookie and then moving it back into place.
wow.... that is a good idea. i will likely adopt that idea. i just got word that if absolutely necessary, database storage could be an alternative option. thank you for the help Renesis and Pekka!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.