Link to home
Start Free TrialLog in
Avatar of Bl248
Bl248

asked on

Using Multiple cookies or 1 cookie with multiple values

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.
Avatar of lozloz
lozloz

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
Avatar of Bl248

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of lozloz
lozloz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bl248

ASKER

Thanks thats what I will do -- use separate cookies.