We help IT Professionals succeed at work.

Using Multiple cookies or 1 cookie with multiple values

Bl248
Bl248 asked
on
362 Views
Last Modified: 2008-03-03
Hi,

I am wondering what best practices would be recommended for storing several user preferences in cookies.

If I have 3 settings like "homepage, theme, feature1, feature2"
Would I set 4 cookies or use a string version of an array to store the 4 variables and then parse the values out upon initialization.

Thanks.
Comment
Watch Question

Commented:
you can use an array of cookies:

setcookie("settings[homepage]", "value", time() + 840000);
setcookie("settings[theme]", "value", time() + 840000);
setcookie("settings[feature1]", "value", time() + 840000);
setcookie("settings[feature2]", "value", time() + 840000);

and then:

print $_COOKIE["settings"]["homepage"];
print $_COOKIE["settings"]["theme"];
print $_COOKIE["settings"]["feature1"];
print $_COOKIE["settings"]["feature2"];

cheers,

loz

Author

Commented:
Thanks I had tried to use an array for the cookie but was creating the array and then trying to set the cookie  as in
setcookie("myarray", $array, time() + 840000);

So I've used your format and I now have a array called $settings with 4 elements. It still appears that I have 4 cookies created when I view the cookies.

I think it may bel better to use the format of creating a pseudo array for multiple cookie values to just creating standard cookies as in homepage, theme, feature1, feature2.  At least it may be more descriptive for anyone viewing the cookies.

Is that what other developers tend to do as a best practice?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks thats what I will do -- use separate cookies.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.