Method
Session
Description
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
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
Note
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
#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 returns Nil when there is no session context available. Whether a session context is present depends not on which thread the code runs on, but on the type of object whose event is firing.
Events from classes with a Web prefix — such as WebThread, WebTimer, WebButton, and other web UI controls — will have a session context available when their events fire. The framework takes care of restoring the session before your code runs, even for objects like WebTimer whose Location property is set to Server, which runs on the main thread.
Events from non-web objects — such as Thread, Timer, Shell, URLConnection, and SerialConnection — will not have a session context by default when their events fire, so Session will be Nil in those contexts. Code run directly in the App object is also in this category.
There are less obvious cases where a session context may be present. For example, if a WebButton Pressed event (which has a session context) synchronously executes a XojoScript, the XojoScript.Print event may also have a session context as a direct result.
If you need to access a Session from a context where it would otherwise be Nil, refer to the Reconnecting to a WebSession topic to learn how to restore a session context using a Session Identifier.
As long as a Session is not Nil, it is 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))
Compatibility
Project Types |
Web |
Operating Systems |
All |
See also
WebSession, WebSessionContext, WebApplication classes