ASP Session Object

Het Session object wordt gebruikt om gebruikersinformatie op te slaan. De variabelen die in het session object worden opgeslagen, bevatten informatie van een enkele gebruiker en zijn beschikbaar op alle pagina's van een applicatie.

Session object

Wanneer u een applicatie gebruikt, opent u deze, maakt u enkele wijzigingen en sluit u deze dan weer. Dit lijkt veel op een gesprek (Session). De computer weet wie u bent. Hij weet wanneer u de applicatie opent en sluit. Maar er is een probleem op het internet: omdat HTTP-adressen de staat niet kunnen behouden, weet de webserver niet wie u bent en wat u hebt gedaan.

ASP lost deze problemen op door voor elke gebruiker een unieke cookie te maken. De cookie wordt naar de client verzonden en bevat informatie die de gebruiker herkent. Dit type interface wordt de Session-object genoemd.

The Session object is used to store information about the user, or to change the settings for a user's session. The variables stored in the session object contain information about a single user and are available on all pages in the application. The information stored in the session object is usually name, id, and parameters. The server creates a new Session for each new user and revokes the Session object when the session expires.

When does the session start?

When does the session start?

  • When a new user requests an ASP file, and the Global.asa file references the Session_OnStart subroutine;
  • When a value is stored in a Session variable;
  • When a user requests an ASP file, and Global.asa uses the <object> tag to instantiate an object through the session scope;

When does the session end?

If the user does not request or refresh the page within the specified time in the application, the session will end. The default value is 20 minutes.

If you want to set a longer or shorter timeout interval, you can set Timeout Property.

The following example sets a timeout interval of 5 minutes:

<%
Session.Timeout=5
%>

To end the session immediately, you can use Abandon Method:

<%
Session.Abandon
%>

Note:The main issue with using session is when they should end. We will not know whether the user's latest request is the final request. Therefore, we are not sure how long the session should "survive". Waiting too long for an idle session will exhaust the server's resources. However, if the session is deleted too early, the user will have to start over again and again, because the server has deleted all the information. Finding the appropriate timeout interval is very difficult.

Tip:If you are using session variables, do not store a large amount of data in them.

Storing and retrieving session variables

The biggest advantage of the Session object is that variables can be stored in it for subsequent web pages to read, and its application range is very wide.

The following example assigns the value "Donald Duck" to a session variable named username, and assigns the value "50" to a session variable named age:

<%
Session("gebruikersnaam")="Donald Duck"
Session("leeftijd")=50
%>

Once a value is stored in a session variable, it can be used by any page in the ASP application:

Welkom <%Response.Write(Session("gebruikersnaam"))%>

Het resultaat van deze regel is: "Welkom Donald Duck".

Je kunt ook gebruikersparameters in het sessie object opslaan en deze parameters gebruiken om te beslissen welke pagina je aan de gebruiker teruggeeft.

Het volgende voorbeeld bepaalt dat als de gebruiker een lage schermresolutie gebruikt, een puur tekstversie van de pagina wordt geretourneerd:

<%Als Session("schermresolutie")="laag" dan%> 
  Dit is de tekstversie van de pagina
<%anders%> 
  Dit is de multimedia versie van de pagina
<%Einde als%>

Verwijder sessie variabele

De contents-collectie bevat alle sessie variabelen.

Je kunt de remove-methode gebruiken om sessie variabelen te verwijderen.

In het volgende voorbeeld, als de waarde van de sessie variabele "leeftijd" kleiner is dan 18, wordt de sessie variabele "verkopen" verwijderd:

<%
Als Session.Contents("leeftijd")<18 dan 
  Session.Contents.Remove("verkopen")
Einde als 
%>

Als je alle variabelen uit de sessie wilt verwijderen, gebruik dan de RemoveAll-methode:

<%
Session.Contents.RemoveAll()
%>

Blader door de contents-collectie

De contents-collectie bevat alle sessie variabelen. Je kunt door de contents-collectie bladeren om de opgeslagen variabelen te bekijken:

<%
Session("gebruikersnaam")="Donald Duck"
Session("leeftijd")=50
dim i
For Each i in Session.Contents
  Response.Write(i & "<br />")
Next
%>

Resultaat:

gebruikersnaam
leeftijd

Als je de hoeveelheid items in de contents-collectie wilt weten, kun je de count-eigenschap gebruiken:

<%
dim i
dim j
j=Session.Contents.Count
Response.Write("Sessie variabelen: " & j)
Voor i=1 tot j
  Response.Write(Session.Contents(i) & "<br />")
Next
%>

Resultaat:

Sessie variabelen: 2
Donald Duck
50

Doorloop de StaticObjects-collectie

Door de StaticObjects-collectie te doorlopen, kun je de waarden van alle objecten die opgeslagen zijn in de session-object bekijken:

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