Did you find this useful?
Socialize it today.


ASP Session Object

Thursday, August 23, 2012

tags ASP

The ASP Session Object is a very valuable tool. It mainly allows you to store information specific about each visitor while there are actively accessing your website. Information about the user such as their username, role, and shopping cart contents are just a few of the things commonly tracked when your visitors access your pages. The session object allows you to store this information and access it from the different pages on your site so you do not have to be concerned with passing this information from page to page as you would with a typical query string or form data. The session object remains available until session expires (20 minutes by default) or you programmatically abandon the session variable.

Since HTTP does not maintain state across pages, we can use the session object to store information. ASP solves this problem by creating a unique cookie for each user. The cookie is sent to the user's computer and it contains information that identifies the user. This interface is called the Session object. As previously mentioned, information stored in the Session object holds information about one single user, and are available to all pages in one application. Common information stored in session variables are name, id, and preferences. The server creates a new Session object for each new user, and destroys the Session object when the session expires.

The Session object's collections, methods, properties, and events are as follows:

Collections

CollectionDescription
ContentsContains all the items appended to the session through a script command
StaticObjectsContains all the objects appended to the session with the HTML <object> element (i.e. in global.asa)


Methods

MethodDescription
AbandonDestroys a user session
Contents.RemoveDeletes an item from the Contents collection
Contents.RemoveAll()Deletes all items from the Contents collection


Properties

PropertiesDescription
Contents.CountReturns the number of items in the Contents Collection
SessionIDReturns a unique id generated by the server for a user.
TimeoutSets or returns the timeout period in minutes for the Session object


Events

EventDescription
Session_OnStartOccurs when a session starts
Session_OnEndOccurs when a session ends


Examples

Store and Retrieve Session Variables

The most common uses for the session object are to store and retrieve information. You create and/or modify session variable information by specifying the variable name within quotes. If you assign string data to the session, encapsulate that data in quotes as well. If you want to assign numeric data, quotes are not needed.


<%
Session("username")="jsmith"
Session("userid")=1204
%>

<%
Response.Write(Session("username"))
%>


Remove Session Variables

You can easily remove one variable from the Contents collection or all of the variables using one command. Use either the Remove or RemoveAll method.


<%
Session.Contents.Remove("userid")
Session.Contents.RemoveAll()
%>


Session Timeout

The default timeout for a user session is 20 minutes of inactivity. This interval can be changed using the Timout Property. In addition, you can abandon the session using the Abandon method.


<%
Session.Timeout=10
Session.Adandon
%>


Global.asa Events

In this next example, we can use the Session_OnStart and Session_OnEnd events (defined in the Global.asa file) to maintain a count of the active users accessing our web server.


<script language="vbscript" runat="server">
Sub Session_OnStart
  Application.Lock
  Application("users") = Application("users") + 1
  Application.UnLock
End Sub

Sub Session_OnEnd
  Application.Lock
  Application("users") = Application("users") - 1
  Application.UnLock
End Sub
</script>

In your ASP page...


<%
    Response.Write("There are " & Application("users") & " users online.")
%>


Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

Sams Teach Yourself Active Server Pages 3.0 in 21 Days Beginning ASP Databases ASP In A Nutshell