Internet

Active Server Pages Tutorial

Accessing Server Variables

Variables residing on a web server can store such information as the name and IP address of the server, the software used by the server, a client’s IP address, a client’s browser type, and the name and location of the ASP page. You cannot change the values contained in these variables (without changing the server configuration), but you can access them using the Request.ServerVariables statement:

<%=Request.ServerVariables(“SERVER_NAME”)%> <%=Request.ServerVariables(“LOCAL_ADDR”)%> <%=Request.ServerVariables(“SERVER_SOFTWARE”)%> <%=Request.ServerVariables(“APPL_PHYSICAL_PATH”)%> <%=Request.ServerVariables(“REMOTE_ADDR”)%> <%=Request.ServerVariables(“HTTP_USER_AGENT”)%> <%=Request.ServerVariables(“ALL_HTTP”)%>

There are many more server variables, which you can view using a FOR.EACH.NEXT loop (For Each Item In Server.Variables.Response.Write(Item)).

The Session Object

The Session object stores session information for individual users. A session is created when a user enters a web site and exists on the server until the user closes the browser or the session expires. In a web server cluster (multiple web servers working in tandem to handle high-traffic web sites), session information is lost when the user moves to a different server. Session variables let you store information for the user, such as user name and preferences, which can be accessed by all pages in the site. When the session ends, the variables are lost. Note: The user’s browser must support cookies for session variables to work.

Session Object: Methods
Session.Abandon Ends the session.
Session Object: Properties
Session.CodePage Specifies the character set used for the ASP code.
Session.LCID Specifies a location, which determines how content such as date and time will be displayed.
Session.SessionID The unique identifier given by the server to each user.
Session.Timeout Specifies (in minutes) how long session information for a user is stored on the server (after the last client request).
Session Object: Collections
Session.Contents Contains items added to the Session object using script.
Session.StaticObjects Contains items added to the Session object using the HTML
Session Object: Events
Session.OnStart Specifies an event to occur when the server creates a session.
Session.OnEnd Specifies an event to occur when a session ends.

Using Session Variables

To create a session variable, use Session(“variable”):

<% Session(“UserName”)=”JaneDoe” %>

Normally, you collect the values of session variables from forms completed by users. If, for example, you use a login page to collect a user name and password, you could post the page to another ASP page that creates and stores the information in session variables:

<% Session(“UserName”)=Request.Form(“txtUserName”) Session(“Password”)=Request.Form(“txtPassword”) %>

You access the session variables in the same way:

<% Session(“UserName”)=Request.Form(“txtUserName”) Session(“Password”)=Request.Form(“txtPassword”) %>

Welcome, <%=Session(“UserName”)%>!

You can access session variables from any ASP page in the site.

Specifying Session Timeout

Use the Session.Timeout statement to view and change timeout settings for each session. The following statement displays the number of minutes a session currently remains in server memory (the default is usually 20 minutes):

<% Response.Write(Session.Timeout)%>

You may want to shorten the timeout period to increase security on your site; for example, you may want the site to “log out” for the user after 5 minutes of inactivity:

<% Session.Timeout=5 %>

Abandoning the Session

When a user logs out of the site, you can use the Session.Abandon statement to end the session immediately, thus freeing server resources:

<% Session.Abandon %> Thank you for visiting!