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:
|Contents||Contains all the items appended to the session through a script command|
|StaticObjects||Contains all the objects appended to the session with the HTML <object> element (i.e. in global.asa)|
|Abandon||Destroys a user session|
|Contents.Remove||Deletes an item from the Contents collection|
|Contents.RemoveAll()||Deletes all items from the Contents collection|
|Contents.Count||Returns the number of items in the Contents Collection|
|SessionID||Returns a unique id generated by the server for a user.|
|Timeout||Sets or returns the timeout period in minutes for the Session object|
|Session_OnStart||Occurs when a session starts|
|Session_OnEnd||Occurs when a session ends|
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.
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.
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.
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">
Application("users") = Application("users") + 1
Application("users") = Application("users") - 1
In your ASP page...
Response.Write("There are " & Application("users") & " users online.")
Recommended Books & Training Resources