Free Guides
Language Tutorials

ASP ( Active Server Pages )
Index

Session Variable
Session ID and Object in ASP
Session object in ASP are there to store user
specific information. In most cases sessions are used to maintain
state as the user navigates through different pages. Some data we
can pass by using query string, web forms and cookies but session
variables has distinctive role than other ways while carrying data
to different pages.
Say we want to carry the member login status to different pages and
with this information the member can enter to members only pages.
Here the session variables are stored at server side ( not at
client side ) and it is connected to user browser. As long as the
browser window is in contact with server, session details will be
available and if there is no interaction from user end within a
specific time the sessions variables will be destroyed. The user
also can end or abandon the session. Let us learn some details about
session object here.
Session ID
Session ID is the unique link between browser and the server.
Once the session is created a session ID is generated which
is a unique number and it is kept at user end ( client browser )
using cookies. Server keeps all the variables corresponding to this
session ID at server side. Each time browser moves to different
pages it submit session ID to server and server after matching
returns all the session variables associated with this session ID.
This session ID is also used in many cases to identify and keep
track of user activity. For example in a shopping cart script we can
keep all the purchases done by the visitor in a temporary table with
a unique field storing the session ID. Based on this session ID what
are the purchases and the total quantity, packets etc can be
calculated at the checkout time. Session ID is the best way to
maintain unique link with the server as many other visitors will be
using the same site and doing purchasing at the same time. Here
Session ID acts like the visitors unique shopping cart. Here is an
example on how to get browser session ID
Response.Write Session.SessionID
When we should not use Session.
Sessions are used to store user specific information so it should
not be used to store global information. For example there is an
announcement for all the members that site member area will not be
available during a particular period due to schedule maintenance.
This message is for all so we should not use session to store this
message. In another case we are using a membership system where user
login id is to be available inside other pages, here details are
user specific so they are to be stored using session
Load on Server
All session variables are stored at server end. So each session of
the visitor adds load to the server resources as server has to
maintain the session variables. Say for example we are using ten
variables for each session of the visitor. Now at a moment if one
thousand visitors logs in to the server then total session variables
will be 10 x 1000. Imagine if the site became more popular and with
growing visitor load on server also will go up. So only user
specific requirements are to be kept in session and special care is
to be taken up for high traffic sites.
Security
This is the biggest advantage of using session. As the data reside
at server side so it remains secure irrespective of user browser
environment. Some cases like user authentication results, passwords
etc are kept in sessions only as users are expected to use different
computers at different locations so they can't be left at client
side. Query string data gets easily exposed by checking the history
of the browser.
Amount of Data
Better not to load the server with more session data as it drains
the server resources. This is important particularly for high
traffic sites. Now temporary tables are available in databases which
are fast and maintain overall small size for database.
We will learn more on session start, session variable and session
time out.
Creating Session arrasy and variables in ASP
At any time we can create a session variable and
store data in it. Let us try to store user name in our session
variable. We have given the name of our session variable as name
only. Here is the example
session("name")=name
Note that the variable name has the name of the
user and now it is available as a session variable. Let us learn how
to display this session variable.
Response.Write "Welcome " & session("name")
Above line will display a welcome message with the
user name. Now let us try to display how many session variables are
present for the user or the total number of session variables
present for the user.
The above line will display a number as total
number of session variables present in our user session.
Removing all values from Session Variables
We can destroy or remove all session variables like this .
Session.Abandon
OR
Selectively removing session values
There may be a requirement of selectively removing some session
variables. Let us learn that . Here is an example of removing one
particular session variable.
ASP Session array
We can store array in our session and carry that to different pages
in ASP. We will learn how to assign a session array from a local
array. Display all elements from a session array and how to assign a
local array from session array.
Let us take the example of a simple array and assign some elements
to it.
language(0)="ASP"
language(1)="PHP"
language(2)="JavaScript"
language(3)="Perl"
For our learning let us display all elements of
the array once.
For i = LBound(language) to UBound(language)
Response.Write language(i) & "<br>"
Next
Same way we will be displaying our session arrays
also so a better understanding from the beginning will help. Now Let
us assign our session variable to this array.
Now we have our array in our session variable. Let us try to display
elements of the session array.
For i =
LBound(session("my_languages")) to UBound(session("my_languages"))
Response.Write session("my_languages")(i) & "<br>"
Next
The above code will display our session elements.
Now let us move to another page and check our session array. You can
write the same ( above ) code and display the elements. Now let us
learn how to transfer the session array to a local array.
my_languages2=session("my_languages")
Now we have our my_languages2 as local
array storing the value from session array. Now we will loop through
and display all the elements of this local array. Before that we
will use one if condition checking to know our variable is an array
or not .
for i=LBound(my_languages2) to UBound(my_languages2)
Response.Write my_languages2(i) & "<br>"
Next
Else
Response.Write " not an array"
End If
Session Time Out or duration of active session in ASP
As the session variables are stored at server side
we can't keep them for ever. Then how long we can keep the session
variables at our server end ? It depends on how frequently or how
long the user is interacting with the server. If the interacting
period is going on then server has to keep the session variables
active but if the user is not active then it will remove the session
variables.
The duration within which the visitor must interact with server to
keep the session live is 20 minutes by default. But this duration
can be changed at script end by this command.
Session.Timeout = 10
The above line will fix the interval for 10 minute and within this time the visitor has to interact with the server to keep the session live. So inside our script we can manage the session time as per our requirement. We can display the session timeout value like this.