Search code examples
phpcookiesdnssetcookie

PHP - Create multiple cookie files


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


Solution

  • 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