Session

From Xojo Documentation

Class (inherits from WebSession)


New in 2010r4

Session refers to two things in your web projects. The Session class in the project is a subclass of WebSession that you can use to add your own properties, methods and constants. Each user that connects to the web app gets their own instance of the Session class, which can be conveniently accessed using the Session method. To learn about the Session class, refer to the WebSession class.

This topic discusses the Session method that provides you with a reference to WebSession for the active user.

fa-info-circle-32.png
To refer to the current Session from within the Session object, use Self instead of Session as the prefix, especially in the Open event because it may return Nil.

Syntax

Session.property = value
or
Session.method

Part Description
Property Any valid property of the WebSession class or the Session subclass in the project.
Value A value of the proper datatype for the Property.
Method Any valid method of the WebSession class or the Session subclass in the project.

Notes

fa-info-circle-32.png
To refer to the current Session from within the Session object, use Self instead of Session as the prefix, especially in the Open event because it may return Nil.

In order to see WebSession properties in the debugger, you can create a temporary variable:

#If DebugBuild Then
Dim currentSession As Session = Session
#Endif

The reason for the pragma is that the call can be very expensive when there are lots of sessions running. This way it'll only affect performance when you're debugging.

Session Context

Session may return Nil if the code is running on the main thread. This includes code that is run in the App object and code that is not part of the user interface, such as code in a Thread.

To determine if a session is available for use, call WebSession.Available rather than comparing against Nil, as the Available method is more efficient and about four times faster.

If Session.Available Then
Dim user As String
user = Session.UserName // your own property added to Session
End If

Multiple Browser Windows

If the user opens two browser windows/tabs to an app, the new window gets its own Session instance. Use cookies to determine if two sessions belong to the same user.

Examples

This example saves the User Name and Password from a login page to properties added to the Session class:

Session.UserName = UserNameField.Text
Session.Password = PasswordField.Text

This code gets the current session associated with a web page:

Dim sc As WebSessionContext
sc = New WebSessionContext(App.SessionForControl(Self))

See Also

WebSession, WebSessionContext, WebApplication