Latest web development tutorials

ASP Session Object

Session object is used to store information about a user session (session), or change the user session (session) setting.


Session Object

When you operate an application on your computer, you open it, do some changes, and then close it. It's like a conversation (Session). The computer knows who you are. It is clear that you open and close applications when. However, on the Internet, the question arises: could not hold because the HTTP address, Web server does not know who you are and what you did.

ASP by creating a unique cookie for each user to solve this problem. cookie is transferred to the user's computer, which contains identifiable user information. This interface is called the Session object.

Session object is used to store information about a user session (session), or change the user session (session) setting.

Variable stores a single user's information is stored in the Session object, and are available for applications in all pages. Stored in a public information session variable is usually name, id and parameters. The server creates a new Session for each new user, and to withdraw out of the Session object when the session expired.


Session When to start?

Session starts at:

  • A new user requests an ASP file, and the Global.asa file references Session_OnStart subroutine
  • A value stored in the variable Session
  • A user requests an ASP file, and the Global.asa use <object> tag by session's scope to instantiate an object

Session will end?

If the user does not request or refresh the page within the time specified in the application, session will end. The default is 20 minutes.

If you want to set the timeout interval to a shorter or longer than the default value, you can use theTimeout property.

The following example sets a timeout interval of 5 minutes:

<%
Session.Timeout=5
%>

To end the session immediately, please use theAbandon method:

<%
Session.Abandon
%>

Note: When using session The main problem is that when they end.We do not know the user's most recent request is the last request. Therefore, we do not know the make session "survive" long. For a free session to wait too long it will run out of server resources. However, if the session is removed prematurely, the user would have to start over and over again, this is because the server has deleted all the information. Finding the right timeout interval can be difficult!

TipTip: In thesession variable to store only a small amount of data!


Session variables to store and retrieve

Session object is the biggest advantage is the variable in which to store for subsequent pages to read, its application range is very wide.

The following examples of the "Donald Duck" Session assigned to the variable namedusername,and "50" is assigned to a variable namedageof Session:

<%
Session("username")="Donald Duck"
Session("age")=50
%>

When the value is stored in the session variable, it can be ASP applications to use any page:

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

Results above this line of code returns: "Welcome Donald Duck".

You can also store user parameters in the Session object, and then access these parameters to determine what the page is returned to the user.

The following example provides that if the user uses a low-resolution display, plain text version of the page is returned:

<%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 Session Variables

Contents collection contains all session variables.

Session variables can be removed by the Remove method.

In the example below, if the "age" of the value of the session variable is less than 18, then remove the session variable "sale":

<%
If Session.Contents("age")<18 then
Session.Contents.Remove("sale")
End If
%>

To remove all of the session variable, use RemoveAll methods:

<%
Session.Contents.RemoveAll()
%>


Traversal Contents collection

Contents collection contains all session variables. You can traverse the Contents collection, to see which stores variables:

<%
Session("username")="Donald Duck"
Session("age")=50

dim i
For Each i in Session.Contents
Response.Write(i & "<br>")
Next
%>

result:

username
age

If you do not know the number of items in the Contents collection, you can use the Count property:

<%
dim i
dim j
j=Session.Contents.Count
Response.Write("Session variables: " & j)
For i=1 to j
Response.Write(Session.Contents(i) & "<br>")
Next
%>

result:

Session variables: 2
Donald Duck
50


Traversal StaticObjects collection

You can traverse StaticObjects collection, to see the values ​​of all objects stored in the Session object:

<%
dim i
For Each i in Session.StaticObjects
Response.Write(i & "<br>")
Next
%>