WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

ASP Session Object


A Session object stores data about, or change settings for a client session.


The Session object

When you are working with an application on your PC, you open it, do a few changes and after that you close it. This is much similar to a Session. The PC knows your identity. It knows when you open the application and when you close it. In any case, on the web there is one issue: the web server does not know your identity and what you do, in light of the fact that the HTTP address doesn't look after state.

ASP takes care of this issue by making a one of a kind treat for every client. The treat is sent to the client's PC and it contains data that recognizes the client. This interface is known as the Session object.

The Session object stores data about, or change settings for a client session.

Variables put away in a Session object hold data around one single client, and are accessible to all pages in a single application. Normal data put away in session factors are name, id, and inclinations. The server makes another Session object for each new client, and devastates the Session object when the session terminates.


When completes a Session Start?

A session begins when:

  • A new client asks for an ASP record, and the Global.asa document incorporates a Session_OnStart procedure
  • A esteem is put away in a Session variable
  • A client asks for an ASP document, and the Global.asa record utilizes the <object> tag to instantiate an item with session scope

When completes a Session End?

A session closes if a client has not asked for or revived a page in the application for a predetermined period. As a matter of course, this is 20 minutes.

If you need to set a timeout interim that is shorter or longer than the default, utilize the Timeout property.

The model beneath sets a timeout interim of 5 minutes:

<%
Session.Timeout=5
%>

Use the Abandon technique to end a session immediately:

<%
Session.Abandon
%>

Note: The fundamental issue with sessions is WHEN they should end. We do not know whether the client's last demand was the last one or not. So we don't have the foggiest idea to what extent we should keep the session "alive". Hanging tight unreasonably long for an inactive session goes through assets on the server, yet on the off chance that the session is erased too early the client needs to start from the very beginning again in light of the fact that the server has erased all the data. Finding the privilege timeout interim can be difficult!


Store and Retrieve Session Variables

The most imperative thing about the Session object is that you can store factors in it.

The precedent underneath 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 esteem is put away in a session variable it very well may be come to from ANY page in the ASP application:

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

The line above returns: "Welcome Donald Duck".

You can likewise store client inclinations in the Session item, and afterward get to that inclination to pick what page to come back to the user.

The model underneath indicates a content just form of the page if the client has a low screen goals:

<%If Session("screenres")="low" Then%>
  This is the content adaptation of the page
<%Else%>
  This is the media form of the page
<%End If%>

Remove Session Variables

The Contents gathering contains all session variables.

It is conceivable to expel a session variable with the Remove method.

The model beneath expels the session variable "sale" if the estimation of the session variable "age" is lower than 18:

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

To expel all factors in a session, utilize the RemoveAll technique:

<%
Session.Contents.RemoveAll()
%>

Loop Through the Contents Collection

The Contents accumulation contains all session factors. You can circle through the Contents accumulation, to perceive what's put away in it:

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

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

Result:

username
age

If you don't have the foggiest idea about the quantity of things in the Contents accumulation, you can utilize the Count property:

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

Result:

Session factors: 2
Donald trap
50

Loop Through the StaticObjects Collection

You can circle through the StaticObjects gathering, to see the estimations of all items put away in the Session object:

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