Standard Way of Storing User Preferences on Client via Web Application
Asked Answered
I

3

5

I realize this might not be a best fit for SO, so please tell me where I should move/post this if that is the case.

My idea is after a user signs into the system, store the user preferences on the client in the form of cookies. If a user modifies said preferences, update the cookies. I need to do this because some of the preferences are client related and will need to be looked at via JavaScript.

I realize I'll need the preferences stored on the server as well. Just wanting to know if pulling them down into cookies is a good idea. My app is primarily ajax driven so I'd like to pull the preferences down once and just store them. I don't want to push them with each server request.

I'd like to avoid things like Local Storage so that I don't have to worry about browsers as much. Cookies seem to be supported heavily by all browsers.

Anyone concur or have a better way?

Inherit answered 21/3, 2012 at 16:47 Comment(2)
Please review the faq with respect to what kinds of questions should not be asked on SO.Grandiloquence
@BrianDriscoll: He's asking whether it's a good idea or not to store preferences in cookies. It's totally a valid question.Bartlet
H
2

Generally cookies are a good idea, provided that you don't have to store a lot of data. Just few things to keep in mind when using cookies to store stuff:

  • user can edit the preferences by hand so make sure you don't have things like is_root=false without additional server side checks.

  • this can be annoying when user removes cookies and preferences are gone, I would go for a server side preferences storage mirrored to cookies so JavaScript can use them.

Other possibility would be to serve dynamic JavaScript, with inlined preferences, instead of static files - you could serve JavaScript the same as you serve dynamic HTML, but then you have to be careful with caching.

Handed answered 21/3, 2012 at 16:55 Comment(3)
Most of the preferences have to do with modal dialogs being shown automatically and the like. Nothing is security related at all. But these actions are triggered by JavaScript and I don't want to have to do a GET just to decide whether or not I want to display a dialog. Your dynamic JavaScript is an interesting idea. Thanks.Inherit
As jfriend00 suggested you can make a small portion of JavaScript dynamic (just set some variables) and leave the rest in a static file for performance.Handed
I'm accepting this answer as correct because it was the first mention of the dynamic javascript option, which is what I think I Am going with. I'd split the rep if I could.Inherit
E
6

EDIT now that you've changed the question from when we first wrote answers:

If you are storing the preference data on the server, then there is no reason to keep preferences data on the local user's computer between visits As such there is no reason to put the data in a cookie as it just increases the size of every client/server roundtrip and takes storage on the client computer necessarily.

Instead, I would suggest that you simply put a preferences object in the page's javascript like this:

var userPref = {theme: "fun", permitContact: false, lastDisplay: "threaded"};

Then, you can get access to the preference values via javascript from any page with code like this:

if (userPref.lastDisplay == "threaded") {
    // do something
}

Old answer before the question was edited:

If you want the client preferences to work from different browsers that the client might use, then you should store the preferences on your server (highly recommended). You can make them available in the web page at any time, by just including a small amount of javascript in each page that sets the properties of a preferences object to the value of the user's preferences. And, then you can have a preferences page where the user can modify/update the preferences and store the newly changed prefs back on the server again.

Cookies are for temporal state that may get cleared at any time and will only ever work on that particular computer. Plus, if you use cookies and if userA logs out and userB logs in on the same computer, the preferences will be wrong for userB.

Equivocation answered 21/3, 2012 at 16:54 Comment(2)
I've updated my question to the fact that I realize I have to store them on the serverInherit
I think that's what @maarons meant by dynamic JavaScript to pull that information in, which is a good approach. And I don't feel I changed my question. You assumed I didn't want to store things on the server. I just wasn't clear enough.Inherit
H
2

Generally cookies are a good idea, provided that you don't have to store a lot of data. Just few things to keep in mind when using cookies to store stuff:

  • user can edit the preferences by hand so make sure you don't have things like is_root=false without additional server side checks.

  • this can be annoying when user removes cookies and preferences are gone, I would go for a server side preferences storage mirrored to cookies so JavaScript can use them.

Other possibility would be to serve dynamic JavaScript, with inlined preferences, instead of static files - you could serve JavaScript the same as you serve dynamic HTML, but then you have to be careful with caching.

Handed answered 21/3, 2012 at 16:55 Comment(3)
Most of the preferences have to do with modal dialogs being shown automatically and the like. Nothing is security related at all. But these actions are triggered by JavaScript and I don't want to have to do a GET just to decide whether or not I want to display a dialog. Your dynamic JavaScript is an interesting idea. Thanks.Inherit
As jfriend00 suggested you can make a small portion of JavaScript dynamic (just set some variables) and leave the rest in a static file for performance.Handed
I'm accepting this answer as correct because it was the first mention of the dynamic javascript option, which is what I think I Am going with. I'd split the rep if I could.Inherit
B
0

You'll need to store the preferences on the server either way, as you don't want to trouble your user with having to re-create their personal settings every time they sign on with a different computer.

Just store them serverside.

Bartlet answered 21/3, 2012 at 16:54 Comment(1)
I've updated my question to the fact that I realize I have to store them on the server.Inherit

© 2022 - 2024 — McMap. All rights reserved.