Learn what are ASP Session Objects and how they are used?

Learn what are ASP Session Objects and how they are used?

In this post you will learn what is session object in ASP and how it is used?

The session object is used to store the information of a user session and to change its settings.

Session is usually when a user opens a particular application on a system, do some alterations on it and then close it. The system that you are using recognizes you and knows when you open and close the application. But, if we look for same recognition of user over internet, then there is a problem, because the web server does not recognize the user and has no information about its activities. This is because the HTTP address cannot maintain state.

So, the solution to this issue is provided by the ASP. It creates a unique cookie for every user and is sent to the system user is using. This cookie contains the information which actually provides the identification of the user. The session object is basically this interface.

In other words, session object is used to store user information and change the settings for the user session. The variables which are stored in a session object contains the information about single user and it can be accessed by every page in a particular application. The information which are usually stored on session variables are name, id and the user preferences. The server will always create a new session object for every user which is automatically destroyed when the session is finished.

Session Start

The session for any user starts at three occasions; when a user has requested the ASP file and the Session_OnStart procedure is included in Global.asa file, when any value is stored in a session variable, when the uses has requested an ASP file, and the <object> tag is used to instantiate an object with session by a Global.asa file.

Session end

Any user session will end if he / she has not refreshed the web page in the application for a given time. This time is usually 20 minutes by default. But for some reason you want to change this wait or timeout interval to short or long period than you have to use the property known as Timeout property.

For example, consider a following example, in which we have set the timeout interval of 25 minutes:

The example below sets a timeout interval of 5 minutes:

<%

Session.Timeout=25

%>

But what if you want to end the session suddenly, then you will have to use the method known as Abandon. Consider the following example:

<%

Session.Abandon

%>

But we do not know when the user is going to end his / her session. The session ending time is unpredictable from the user side i.e. we do not know that the last request of the user was the final request or not. Thus, you do not know how long you need to keep the session alive. If you wait too long for the user to continue the session then it will use more of your resources which is not practical. On the other hand, if the session is stopped too soon than the user might have to start gain because when we stopped the session the server deleted all of the information of user session. Therefore, determining and setting a right timeout interval is a challenge. Therefore, we recommend to only store small amounts of data in the session variables.

In the next post, you will learn how to retrieve, store and remove the session variables. You will also learn how to loop through the content and static object collections.

email

Leave a Reply