Open Directory Site ASP Tutorials

ASP | XML | VBScript | JavaScript | ADO | CSS | XMLDOM | PHP | Operating Systems

Home >> ASP >>ASP Sessions

 ASP Sessions

 

Sessions

The Session Object is used to store information about, or change settings for a user session. Variables 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 create new Session objects for new users, and destroy Session objects when the session expires.

Start a Session

A session can start when:

· A new user requests an .asp file in an application, and the Global.asa file includes a Session_OnStart procedure

· A user stores a value in the Session Object

· The server receives a request that does not contain a valid SessionID cookie

· A user requests an .asp file in an application, and the Global.asa file uses the <object> tag to instantiate an object with session scope

End a Session

A session ends if a user has not requested or refreshed a page in an application for a specified period. This value is 20 minutes by default.

If you want to set a timeout interval that is shorter or longer than the default, you can set the

"Timeout" property. The example below sets a timeout interval of 5 minutes.

<% Session.Timeout=5 %>

To end a session you can also use the "Abandon" method.

<% Session.Abandon %>

Store and Retrieve Variable Values

You can store variables in the Session Object, like this:

<%

Session("username")="Hege"

Session("age")=24

%>

You can get variable values from the Session Object, like this:

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

The line above returns: "Hege".

You can also store user preferences in the Session Object, and then access that preference to choose what page to return to the user.

The example below specifies a text-only version of the page if the user has a low screen resolution:

<%

If Session("screenres")="low" Then

%>

This is the text version of the page

<%

Else

%>

This is the multimedia version of the page

<%

End If

%>

Remove Variable Values

The Contents collection contains all of the variables that have been stored for a session. By using the Remove method, you can remove a variable from the session.

The example below removes an item, in this case a session variable named "sale":

<%

If Session.Contents("age")<=18 then

Session.Contents.Remove("sale")

End If

%>

You can also use the RemoveAll method to remove all variables in a session:

<% Session.Contents.RemoveAll() %>

Looping Through the Contents

You can loop through the contents collection, to see what is stored in it:

<%

dim i

For Each i in Session.Contents

Response.Write(Session.Contents(i) & "<br>")

Next

%>

Result:

Hege

24

If you do not know how many items that are stored in a contents collection, you can use the "Count" property:

<%

dim i

dim j

j=Session.Contents.Count

For i=1 to j

Response.Write(Session.Contents(i) & "<br>")

Next

%>

Result:

Hege 24

Looping Through the Objects

You can loop through the "StaticObjects" collection, to see the values of all the objects stored in the Session Object:

<%

dim i

For Each i in Session.StaticObjects

Response.Write(Session.StaticObjects(i) & "<br>")

Next

%>

Cheap Web Hosting Articles - Web Site Design & Web Hosting Tutorials - Domain Hosting