Method

# Session

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

<span class="title-ref">Session</span> refers to two things in your web projects. The <span class="title-ref">Session</span> class in the project is a subclass of `WebSession</api/web/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 <span class="title-ref">Session</span> class, which can be conveniently accessed using the <span class="title-ref">Session</span> method. To learn about the <span class="title-ref">Session</span> class, refer to the `WebSession</api/web/websession>` class. This topic discusses the <span class="title-ref">Session</span> method that provides you with a reference to WebSession for the active user.

## Usage

``` xojo
Session.Property = Value
```

or

``` xojo
Session.Method
```

| Part     | Description                                                                                                                              |
|----------|------------------------------------------------------------------------------------------------------------------------------------------|
| Property | Any valid property of the `WebSession</api/web/websession>` class or the <span class="title-ref">Session</span> subclass in the project. |
| Value    | A value of the proper datatype for the Property.                                                                                         |
| Method   | Any valid method of the `WebSession</api/web/websession>` class or the <span class="title-ref">Session</span> subclass in the project.   |

## Notes

<div class="note">

<div class="title">

Note

</div>

To refer to the current <span class="title-ref">Session</span> from within the <span class="title-ref">Session</span> object, use `Self</api/language/self_keyword>` instead of <span class="title-ref">Session</span> as the prefix, especially in the Opening event because it may return `Nil</api/language/nil>`.

</div>

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

``` xojo
#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.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### <span class="title-ref">Session</span> context

<span class="title-ref">Session</span> returns `Nil</api/language/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</api/web/webthread>`, `WebTimer</api/web/webtimer>`, `WebButton</api/user_interface/web/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</api/web/webtimer>` whose `Location` property is set to `Server`, which runs on the main thread.

Events from non-web objects — such as `Thread</api/language/threading/thread>`, `Timer</api/language/timer>`, `Shell</api/os/shell>`, `URLConnection</api/networking/urlconnection>`, and `SerialConnection</api/hardware/serialconnection>` — will not have a session context by default when their events fire, so <span class="title-ref">Session</span> will be `Nil</api/language/nil>` in those contexts. Code run directly in the `App</api/web/webapplication>` object is also in this category.

There are less obvious cases where a session context may be present. For example, if a `WebButton</api/user_interface/web/webbutton>` `Pressed` event (which has a session context) synchronously executes a `XojoScript</api/language/xojo_script/xojoscript>`, the `XojoScript.Print` event may also have a session context as a direct result.

If you need to access a <span class="title-ref">Session</span> from a context where it would otherwise be `Nil</api/language/nil>`, refer to the `Reconnecting to a WebSession</topics/web/reconnecting_to_a_dropped_web_session>` topic to learn how to restore a session context using a Session Identifier.

As long as a <span class="title-ref">Session</span> is not `Nil</api/language/nil>`, it is available for use:

``` xojo
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 <span class="title-ref">Session</span> 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 <span class="title-ref">Session</span> class:

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

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This code gets the current <span class="title-ref">Session</span> associated with a web page:

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

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | Web |
| **Operating Systems** | All |

<div class="seealso">

`WebSession</api/web/websession>`, `WebSessionContext</api/web/websessioncontext>`, `WebApplication</api/web/webapplication>` classes

</div>
