Page 231 - DCAP408_WEB_PROGRAMMING
P. 231
Unit 9: ASP Objects
To end a session immediately, you may use the Abandon method: Notes
<%
Session.Abandon
%>
Notes The main problem with sessions is WHEN they should end. We do not know if the
user’s last request was the final one or not. So we do not know how long we should keep
the session “alive”. Waiting too long for an idle session uses up resources on the server,
but if the session is deleted too soon the user has to start all over again because the server
has deleted all the information. Finding the right timeout interval can be difficult!
Tip: If you are using session variables, store SMALL amounts of data in them.
Store and Retrieve Session Variables
The most important thing about the Session object is that you can store variables in it.
The example below will set the Session variable username to “Donald Duck” and the Session
variable age to “50”:
<%
Session(“username”)=”Donald Duck”
Session(“age”)=50
%>
When the value is stored in a session variable it can be reached from ANY page in the ASP
application:
Welcome <%Response.Write(Session(“username”))%>
The line above returns: “Welcome Donald Duck”.
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 Session Variables
The Contents collection contains all session variables.
It is possible to remove a session variable with the Remove method.
The example below removes the session variable “sale” if the value of the session variable
“age” is lower than 18:
LOVELY PROFESSIONAL UNIVERSITY 225