From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

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.

Usage = value

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.


To refer to the current Session from within the Session object, use Self instead of Session as the prefix, especially in the Opening event because it may return Nil.

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

#If DebugBuild Then
Var currentSession As Session = Session

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.

As long a session is not Nil, it's available for use:

If Not (Session = Nil) Then
Var 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.

Sample Code

This code 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:

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

See Also

WebSession, WebSessionContext, WebApplication classes