How to Use Session States Instead of Cookies

By Socrates

No cookies! No cookies!

Rate: (5 Ratings)

Cookies are old-school. Session states are the new thing. Find out how to use them and join the revolution.

Instructions

Difficulty: Moderately Easy

Things You’ll Need:

  • ASP.NET
  • Microsoft Visual Studio 2005
  • A computer with IIS and ASP2.0 setup and/or
  • A hosting company with ASP.NET support

Step1
Add Global.asax Plan ahead. Session states can be created on the fly, but it's always better to plan their use to maximize their effectiveness. One of the best places to state your session states is in the "Global.asax" file within your web application. To create this file, right-click on your project title in the Solution Explorer and select "Add New Item". From the window, select "Global Application Class".
Step2
Add a Session State The Session States can really be placed anywhere in this file. The most basic spot is the Session_Start. Declare an empty Session State by the following: Session("DescriptiveName") = "". Add as many as you want, even assign values if you know what they are ahead of time.
Step3
Adding the extras In your "Default.aspx" file, write the Session("Greeting") to the screen and drop in a TextBox and button. Lastly, write the Session("UserName") to the screen. In the example, a few more details were added for clarity, including an OnClick function for the button.
Step4
Run the example Run the example. Notice the Greeting state that was declared in "Global.asax" is immediately displayed while the UserName state is not. Now, if you type in the TextBox and click the button, the page will reload and the UserName state will now contain what you typed in the TextBox. In either case, you will have successfully captured, used, and displayed information that never had to be written to a cookie on your guest's computer.

Tips & Warnings

  • This example was created using Visual Basic as the language of choice

Post a Comment

POST A COMMENT

Request a New How-To Article

Looking for more How To information? Chances are there’s an eHow member who knows how to do what you’re looking to do. Submit an article request now!

eHow Article:  How to Use Session States Instead of Cookies

eHow Member: Socrates

Socrates

Enthusiast Enthusiast | 1370 Points

Category: Internet

Articles: See my other articles

Related Ads

Internet

Veesites
Meet Virginia DeBolt eHow’s Internet Expert.