UniSession Object

UniSession returns an instance of the TUniGUISession class for the current session. It contains all the information related to a session (for example: IP Address, User Agent, Host Address, Platform related data, etc.). UniSession also contains important methods which can be used to control a session (for example, Terminate to terminate a session).

Example — terminate current session:

TMainForm.UniButton2Click
procedure TMainForm.UniButton2Click(Sender: TObject);
begin
  UniSession.Terminate; // Terminate current session
end;

Example — redirect current window to a new location:

TMainForm.UniButton2Click
procedure TMainForm.UniButton2Click(Sender: TObject);
begin
  // Redirect current window to a new location
  UniSession.UrlRedirect('http://www.newsite.com');
end;

Like UniApplication, UniSession returns a valid instance only when accessed from a uniGUI control event handler — i.e., controls which belong to a session. For example, a TUniButton instance always belongs to a session:

TMainForm.UniButton2Click
procedure TMainForm.UniButton2Click(Sender: TObject);
var
  IPAddress: string;
begin
  // We are in an event handler from a TUniButton, so UniSession
  // can be accessed here.
  IPAddress := UniSession.RemoteIP;
end;

Warning: accessing UniSession from code that is not associated with a session will fail. In the example below, accessing UniSession will cause an access violation because UniThreadTimer is not a uniGUI control and its events are not associated with any session. UniThreadTimer events run asynchronously in a separate thread.

UniSession is a "global" object which can return different values when accessed from different sessions. This is the same behavior as UniApplication; UniSession is actually a global function declared in uniGUIApplication.pas which returns the correct session instance when called inside an event handler.

Relevant unit excerpt:

Reference link (original documentation): https://unigui.com/doc/online_help/index.html?unisession-object.htm