views:

767

answers:

8

There are various ways to maintain user state using in web development.

These are the ones that I can think of right now:

  1. Query String

  2. Cookies

  3. Form Methods (Get and Post)

  4. Viewstate (ASP.NET only I guess)

  5. Session (InProc Web server)

  6. Session (Dedicated web server)

  7. Session (Database)

  8. Local Persistence (Google Gears) (thanks Steve Moyer) etc.

I know that each method has its own advantages and disadvantages like cookies not being secure and QueryString having a length limit and being plain ugly to look at! ;)

But, when designing a web application I am always confused as to what methods to use for what application or what methods to avoid.

What I would like to know is what method(s) do you generally use and would recommend or more interestingly which of these methods would you like to avoid in certain scenarios and why?

+1  A: 

Personally, since almost all of my web development is in PHP, I use PHP's session handlers.

Sessions are the most flexible, in my experience: they're normally faster than db accesses, and the cookies they generate die when the browser closes (by default).

warren
+1  A: 

With the increasing use of Web 2.0, I think there are two important methods missing from your list:

8 AJAX applications - since the page doesn't reload and there is no page to page navigation, state isn't an issue (but persisting user data must use the asynchronous XML calls).

9 Local persistence - Browser-based applications can persist their user data and state to the local hard drive using libraries such as Google Gears.

As for which one is best, I think they all have their place, but the Query String method is problematic for search engines.

Steve Moyer
Oops... the Google Gears idea did slip my mind. With Chrome getting a bigger and bigger base, I don't think I will be able to let it slip for long!
Adhip Gupta
Oh, nice -- Google Gears is another good way to think about it. I'd argue that it's again very much like session-based state, and would think of it similarly, but it's definitely missing from the list.
delfuego
+3  A: 

While this is a very complicated question to answer, I have a few quick-bite things I think about when considering implementing state.

  • Query string state is only useful for the most basic tasks -- e.g., maintaining the position of a user within a wizard, perhaps, or providing a path to redirect the user to after they complete a given task (e.g., logging in). Otherwise, query string state is horribly insecure, difficult to implement, and in order to do it justice, it needs to be tied to some server-side state machine by containing a key to tie the client to the server's maintained state for that client.
  • Cookie state is more or less the same -- it's just fancier than query string state. But it's still totally maintained on the client side unless the data in the cookie is a key to tie the client to some server-side state machine.
  • Form method state is again similar -- it's useful for hiding fields that tie a given form to some bit of data on the back end (e.g., "this user is editing record #512, so the form will contain a hidden input with the value 512"). It's not useful for much else, and again, is just another implementation of the same idea behind query string and cookie state.
  • Session state (any of the ways you describe) are all great, since they're infinitely extensible and can handle anything your chosen programming language can handle. The first caveat is that there needs to be a key in the client's hand to tie that client to its state being stored on the server; this is where most web frameworks provide either a cookie-based or query string-based key back to the client. (Almost every modern one uses cookies, but falls back on query strings if cookies aren't enabled.) The second caveat is that you need to put some though into how you're storing your state... will you put it in a database? Does your web framework handle it entirely for you? Again, most modern web frameworks take the work out of this, and for me to go about implementing my own state machine, I need a very good reason... otherwise, I'm likely to create security holes and functionality breakage that's been hashed out over time in any of the mature frameworks.

So I guess I can't really imagine not wanting to use session-based state for anything but the most trivial reason.

delfuego
On the other hand, Query string state is the only option for creating a permanent link, and for search-engine optimization.
James Curran
Oh, totally -- didn't include that on my list, but that's a good point.
delfuego
A: 

It's not some much a question of what to use & what to avoid, but when to use which. Each has a particular circumstances when it is the best, and a different circumstance when it's the worst.

The deciding factor is generally lifetime of the data. Session state lives longer than form fields, and so on.

James Curran
+2  A: 

Security is also an issue; values in the query string or form fields can be trivially changed by the user. User authentication should be saved either in an encrypted or tamper-evident cookie or in the server-side session. Keeping track of values passed in a form as a user completes a process, like a site sign-up, well, that can probably be kept in hidden form fields.

The nice (and sometimes dangerous) thing, though, about the query string is that the state can be picked up by anyone who clicks on a link. As mentioned above, this is dangerous if it gives the user some authorization they shouldn't have. It's nice, though, for showing your friends something you found on the site.

Lucas Oman
I believe that there are only two reasons for the use of query strings today - if I want people to land up at a particular place on my webpage on clicking a link and in case I want to authenticate a new user account and he/she clicks a link on the email sent by me.
Adhip Gupta
+1  A: 

Avoid InProc if you plan to host your website on a cheap-n-cheerful host like webhost4life. I've learnt the hard way that because their systems are over subscribed, they recycle the applications very frequently which causes your session to get lost. Very annoying.

Their suggestion is to use StateServer which is fine except you have to serialise/deserialise the session eash post back. I love objects and my web app is full of them. I'm concerned about performance when switching to StateServer. I need to refactor to only put the stuff I really need in the session.

Wish I'd know that before I started...

Cheers, Rob.

Rob Nicholson
Hey Rob, I will surely keep that in mind since quite a few of my projects will be hosted on "cheap-n-cheerful" webhosts! :)
Adhip Gupta
+1  A: 

Be careful what state you store client side (query strings, form fields, cookies). Anything security-related should not be stored client-side, except maybe a session identifier if it is reasonably obscured and hard to guess. There are too many websites that have settings like "authenticated=true" and store those in a cookie or query string or hidden form field. It is trivial for a user to bypass something like that. Remember that ANY input coming from a client could have been tampered with and should not be trusted.

sk
+1  A: 

Signed Cookies linked to some sort of database store when you need to grab data. There's no reason to be storing data on the client side if you have a connected back-end; you're just looking for trouble if this is a public facing website.

Clint Ecker