The 90’s saw many inovations in the web, but not much changed in the way of client side storage. Javascript and cookies were the way to keep user data saved in the browser.
Recently various techniques for storing and retrieving data have emerged. There are so many different ways in fact that we need to answer some questions to determine the best solution for persisting data between requests.
We have to ask who needs the data ? How long does the data need to be persisted? And how large is the set of data?
Persistence Method | Who Needs the Data? | For How Long? | How Much Data? | |
---|---|---|---|---|
Application | All users | Until the next application restart | Can be almost any size—it will only be stored once | |
Cookie | One user | As short as desired, or for months or even years if the user doesn’t delete their cookies | Minimal, simple data | |
Form Post | One user | For the next request (can be reused across many requests) | Virtually any size—the data is sent back and forth with every page | |
QueryString | One user or one group of users | For the next request (can be reused across many requests) | Minimal, simple data | |
Session | One user | As long as the user is active, plus a timeout period (typically 20 minutes) | Can be almost any size, but should be minimized since every user has their own separate session store | |
Cache | All users or a subset of users | As long or as short as needed | Can be used for large or small, simple or complex data | |
Context | One user | This request only | Can hold large objects, but typically does not since it is often used for every request | |
ViewState | One user | One Web form | Minimal; as with Form Post, this data is sent back and forth with every page | |
Config file | All users | Until the configuration file is updated | Can hold a lot of data; usually organized as many small strings or XML structures |
Application
Session
Here is some sample session variable code: Methods
Create session variable and set it:
Session(“varName”) = varValue
Get on page:
newVar = Sesssion(“varName”)
Clear session variable:
Session(“varName”) = “” OR
Session.Contents.Remove(“varName”)
Clear out session when done:
Session.Contents.RemoveAll() OR
Session.Abandon
I would combine the two to ensure that pesky session data is gone.My logout script is :
Session.Contents.RemoveAll()
Session.Abandon
Response.Redirect(“default.asp”)
The session object has the onStart and OnEnd events and the SessionID and Timeout properties
A session ends if a user has not requested or refreshed a page in the application for a specified period. By default, this is 20 minutes. They could need longer for filling out these forms.
If you want to set a timeout interval that is shorter or longer than the default, use the Timeoutproperty.
Session.Timeout=5 gives the session 5 minutes of inactivity. I think you would want a few hours at least.
Here is the best resources for asp dev http://www.w3schools.com/asp/asp_sessions.asp
This is a summary of a microsoft developer network article. that was updated in 2005.
Now lets get into the new technology
Here is my personal test file http://itwebdev.mv.skagit.edu/don/stuff/dom%20storage/domstorage.html
DOM storage, also called Web Storage is comprable to cookies. However, web storage offers more control of how the information is stored and accesed by each window.
This technique also offers a lot more storage space, and quite different functionality than cookies.
The web storage scripting objects have been drafted into the HTML 5 working draft specifications, and have gathered major vendor support.
window.sessionStorage
Session storage is designed for scenarios where the user is carrying out a single transaction. The sessionStorage attribute of the window object maintains key/value pairs for all pages loaded during the lifetime of a single tab (for the duration of the top-level browsing context). For example, a page might have a check box that the user selects to indicate that he wants insurance. A later page could then check, from script, whether the user had selected the check box.
The Storage object supports expando properties (‘insurance’ in the preceding example). If the property name does not exist, a key/value pair is automatically created to hold it. Note that key/value pairs are always stored as strings. Different data types such as numbers, Boolean values, and structured data must be converted to strings before persisting to a storage area.
After a value has been saved to sessionStorage, it can be retrieved by script running in another page in the same context. When another document is loaded,sessionStorage is initialized from memory for same-origin URLs
Note Although it is allowed by the HTML5 (Working Draft), Internet Explorer 8 does not resume sessionStorage after browser crash recovery.
window.localStorage
The local storage mechanism spans multiple windows and persists beyond the current session. The localStorage attribute provides persistent storage areas for domains. It allows Web applications to store nearly 10 MB of user data, such as entire documents or a user’s mailbox, on the client for performance reasons.
here is the msdn article that this blog post sumarrizes http://msdn.microsoft.com/en-us/library/cc197062(v=vs.85).aspx