Go to: Articles List

Session Object Explained

What is a session? A session is started when I user hits your website. A session is ended when a user either closes his/her browser or the session timeout is reached.

There are two properties in the Session Object that you should be concerned about:

Session.SessionID - This will return to you the unique session identifier.
Session.TimeOut - This is the amount of time in minutes that a session will stay alive.

You should not worry about setting or changing the SessionID. This is set automatically and you don't need to worry about it. You can print it out if you are curious as to what it looks like:

Response.Write(Session.SessionID)

The Session.TimeOut is something you might want to change. This time, in minutes, is how long your session variables will be accessible. Unless the user closes their browser, then all session variables will be cleared. You can change the Session.TimeOut like this:

Session.TimeOut = NumberOfMinutes The default for the Session.TimeOut is 30 minutes.

Now...Session Variables, this is where the power comes in. With Session Variables you can set a variable on one page and access it on another page. An example of when this is used is when you log in to a website a session variable is set. Then as you surf throughout the site, especially to areas where you have to be logged in, the server knows that you have already logged in. This way you don't have to retype in your username and password for every page you go to.

You declare and set Session Variables like this:

Session("VarName") = "value"

They are then accessed like this:

Response.Write(Session("VarName"))

Keep in mind that Session Variables are stored in memory on your server. Therefore you want to use them sparingly because they can slow down your server if you use to many.