user settings cookie
Sometimes in applications you will have certain user settings that you want to apply, even when the user is not logged in. Take for instance these examples:
- “welcome back <name>” msg on return.
- You have a portal type page where the user can control what content is shown where
- You want to track where the user was when he last visited the site, perhaps to offer him the option to return to there.
I recently needed some functionality like that. So I’ve created a object that can help me with that.
I thought about it for a moment and created a singleton settings object for me to call upon to set and retrieve certain settings. Now I have to warn you that there is a small problem with singletons, if you use unit testing it can be difficult to control the behaviour of singletons over multiple tests. So be wary of this when you are running unit tests.
I also wrap all data in a separate array. This isn’t really necessary, but it makes handling the data a lot easier. If you wanted you could also add some sort of encryption to the cookie data so that users couldn’t easily tamper with it.