program routingsessiondemo; {$mode objfpc}{$H+} uses SysUtils, fphttpapp, // Standaline fphttp, // Session manager iniwebsession, // Ini based sessions httpdefs, // TRequest,TResponse httproute; {$IFDEF VER3_0} Type { TSessionState } TSessionState = Class(TObject) IsNew : Boolean; IsExpired : Boolean; Procedure OnNew(Sender : TObject); Procedure OnExpired(Sender : TObject); end; procedure TSessionState.OnNew(Sender: TObject); begin IsNew:=True; end; procedure TSessionState.OnExpired(Sender: TObject); begin IsExpired:=True; end; {$ENDIF} Procedure DisplayForm(aResponse : TResponse); begin With AResponse.Contents do begin Add('

Set variable value:

'); Add('The value of a variable can be stored in the session.
Here you can set the value of the variable.

'); Add('

'); Add('

'); Add(''); Add('

'); Add('Start new session

'); Add('End the session'); end; end; Procedure DisplayNewSession(aResponse : TResponse; aSession : TCustomSession); Var C : TCookie; begin With AResponse.Contents do begin Add('Demo session was started'); Add('

New session started

'); {$IFNDEF VER3_0} if (ssExpired in aSession.SessionState) then Add('The session associated with the cookie the browser sent, has expired.

'); {$ENDIF} Add('A new session was started.

'); C:=AResponse.Cookies.FindCookie(aSession.SessionCookie); If Assigned(C) then begin Add('The issued session cookie is called '+C.Name+'.
'); Add('The issued session cookie has value '+C.Value+'.
'); end else Add('No session cookie was found.'); DisplayForm(AResponse); Add(''); end; end; Procedure NewSession(aRequest : TRequest; aResponse : TResponse); Var Session : TCustomSession; begin Session:=SessionFactory.CreateSession(aRequest); try Session.InitSession(aRequest,Nil,Nil); Session.InitResponse(aResponse); DisplayNewSession(aResponse,Session); Finally Session.Free; end; end; Procedure EndSession(aRequest : TRequest; aResponse : TResponse); Var Session : TCustomSession; begin Session:=SessionFactory.CreateSession(aRequest); try Session.InitSession(aRequest,Nil,Nil); // Stop the session Session.Terminate; Session.InitResponse(aResponse); With AResponse.Contents do begin Add('Demo Session Is Terminated'); Add('

Demo session Terminated

'); Add('The session was terminated, the cookie is cleared and the'); Add('stored value is lost

'); Add('Start new session'); Add(''); end; finally Session.Free; AResponse.SendContent; end; end; Procedure InSession(aRequest : TRequest; aResponse : TResponse); Var V : string; C : TCookie; Session : TCustomSession; NewSession : Boolean; {$IFDEF VER3_0} State : TSessionState; {$ENDIF VER3_0} begin {$IFDEF VER3_0} State:=nil; {$ENDIF VER3_0} Session:=SessionFactory.CreateSession(aRequest); try {$IFDEF VER3_0} State:=TSessionState.Create; Session.InitSession(aRequest,@State.OnNew,@State.OnExpired); NewSession:=State.IsNew; {$ELSE} Session.InitSession(aRequest,Nil,Nil); NewSession:=ssNew in Session.SessionState; {$ENDIF} Session.InitResponse(aResponse); // The url was called, but there was no session yet or it expired... if NewSession then DisplayNewSession(AResponse,Session) else With AResponse.Contents do begin Add('Demo session active'); Add('

Demo session active

'); Add('The demo session is still active

'); // If Session is TFPWebSession then begin C:=AResponse.Cookies.FindCookie(Session.SessionCookie); If Assigned(C) then begin Add('Current session Cookie is called '+C.Name+'
'); Add('and has value '+C.Value+'.'); end; V:=Session.Variables['Var']; If (V<>'') then Add('

Stored session value: '+V+'.') else Add('

No values stored in session.'); V:=ARequest.QueryFields.Values['Var']; If V<>'' then begin Add('

Storing new session value: '+V+'.'); Session.Variables['Var']:=V; end; end; DisplayForm(AResponse); Add(''); AResponse.SendContent; // Handles the response. end; finally {$IFDEF VER3_0} State.Free; {$ENDIF} Session.Free; end; end; begin HTTPRouter.RegisterRoute('/insession',@InSession); HTTPRouter.RegisterRoute('/endsession',@EndSession); HTTPRouter.RegisterRoute('/newsession',@NewSession,True); if ParamCount=1 then Application.Port:=StrToIntDef(ParamStr(1),8080) else Application.Port:=8080; Writeln('Server listens on port : ',Application.Port); Application.Initialize; Application.Run; end.