Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparison of ways to maintain state

Tags:

state

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?

like image 691
Adhip Gupta Avatar asked Sep 18 '08 18:09

Adhip Gupta


People also ask

Which techniques are used in PHP for maintaining States?

The best way to maintain state with PHP is to use the built-in session-tracking system. This system lets you create persistent variables that are accessible from different pages of your application, as well as in different visits to the site by the same user.

How do you maintaining state in PHP explain with example?

HTTP is a stateless protocol, which means that once a web server completes a client's request for a web page, the connection between the two goes away. In other words, there is no way for a server to recognize that a sequence of requests all originate from the same client. State is useful, though.


2 Answers

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.

like image 130
delfuego Avatar answered Sep 19 '22 18:09

delfuego


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.

like image 36
Lucas Oman Avatar answered Sep 19 '22 18:09

Lucas Oman