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.
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 the session expires (20 minutes by default) or you programmatically abandon the session variable.
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 contains information that identifies the user. This interface is called the
As previously mentioned, information stored in the
Session object holds information about one single user and is 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.
Session object‘s collections, methods, properties, and events are as follows:
|Contains all the items appended to the session through a script command|
|Contains all the objects appended to the session with the |
|Destroys a user session|
|Deletes an item from the Contents collection|
|Deletes all items from the Contents collection|
|Returns the number of items in the Contents Collection|
|Returns a unique id generated by the server for a user|
|Sets or returns the timeout period in minutes for the Session object|
|Occurs when a session starts|
|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.
<% 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
<% Session.Contents.Remove("userid") Session.Contents.RemoveAll() %>
The default timeout for a user session is 20 minutes of inactivity. This interval can be changed using the
Timeout Property. In addition, you can abandon the session using the
<% Session.Timeout=10 Session.Adandon %>
In this next example, we can use the
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.") %>