Standard Way of Storing User Preferences on Client via Web Application

梦想与她 提交于 2019-12-10 18:11:34

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/9809118/standard-way-of-storing-user-preferences-on-client-via-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!