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...

Naftali
  • 144,921
  • 39
  • 244
  • 303
adam
  • 2,930
  • 7
  • 54
  • 89
  • It is hard to follow what you are trying to do, please expand on your question, so we can help you. – toneplex May 23 '11 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. – Nicole May 23 '11 at 17:56
  • Also, I feel the most important thing to say is the obligatory "[don't do it](http://meta.stackexchange.com/questions/8891/is-dont-do-it-a-valid-answer)" answer - persist substantial data and you won't have this problem. This is not what cookies were made for. – Nicole May 23 '11 at 18:01

1 Answers1

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

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 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 May 23 '11 at 17:58
  • @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. – Pekka May 23 '11 at 17:59
  • @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. – Nicole May 23 '11 at 18:00
  • 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! – adam May 23 '11 at 18:06