8
Classes, SysUtils, FileUtil, LResources, HTTPDefs, websession, fpHTTP, fpWeb;
14
TFPWebModule1 = class(TFPWebModule)
15
procedure DataModuleAfterResponse(Sender: TObject; AResponse: TResponse);
16
procedure DataModuleCreate(Sender: TObject);
17
procedure DataModuleNewSession(Sender: TObject);
18
procedure DataModuleSessionExpired(Sender: TObject);
19
procedure gotonextpageRequest(Sender: TObject; ARequest: TRequest;
20
AResponse: TResponse; var Handled: Boolean);
22
{ private declarations }
23
NewSessionCreated : Boolean;
24
ASessionExpired : Boolean;
25
procedure GetSessionEvent(Var ASession : TCustomSession);
26
procedure AutoSessionTemplateReplaceTag(Sender: TObject; const TagString:String;
27
TagParams: TStringList; Out ReplaceText: String);
29
{ public declarations }
33
FPWebModule1: TFPWebModule1;
39
procedure TFPWebModule1.DataModuleAfterResponse(Sender: TObject;
40
AResponse: TResponse);
42
//reset global variables for apache modules for the next incoming request
43
NewSessionCreated := false;
44
ASessionExpired := false;
48
procedure TFPWebModule1.GetSessionEvent(Var ASession : TCustomSession);
52
S := TFPWebSession.Create(Nil);
53
S.SessionCookie := 'ACustomCookieName'; {Use this to set the cookie name that will be used for the session management. Default is 'FPWebSession'}
54
// S.SessionDir := '/Path/To/A/Directory/';{Use this if you don't want the automatic Temp dir to store the sessionID files}
55
S.TimeoutMinutes := 2;//with a session timeout of 2 minutes (default is 15)
59
procedure TFPWebModule1.DataModuleCreate(Sender: TObject);
61
NewSessionCreated := false;
62
ASessionExpired := false;
63
Template.AllowTagParams := true;
64
Template.StartDelimiter := '{+'; //The default is { and } which is usually not good if we use Javascript in our templates
65
Template.EndDelimiter := '+}';
66
OnGetDefaultSession := @GetSessionEvent;
67
CreateSession := true; //Turn on automatic session handling for this web module
70
procedure TFPWebModule1.DataModuleNewSession(Sender: TObject);
72
NewSessionCreated := true;
75
procedure TFPWebModule1.DataModuleSessionExpired(Sender: TObject);
77
ASessionExpired := true;
80
procedure TFPWebModule1.gotonextpageRequest(Sender: TObject; ARequest: TRequest;
81
AResponse: TResponse; var Handled: Boolean);
82
begin //Template:TFPTemplate is a property of the web module
83
Template.FileName := 'autosession-template.html';
84
Template.OnReplaceTag := @AutoSessionTemplateReplaceTag;
86
AResponse.Content := Template.GetContent;
91
procedure TFPWebModule1.AutoSessionTemplateReplaceTag(Sender: TObject; const TagString:
92
String; TagParams: TStringList; Out ReplaceText: String);
94
if AnsiCompareText(TagString, 'DATETIME') = 0 then
96
ReplaceText := FormatDateTime(TagParams.Values['FORMAT'], Now);
99
if AnsiCompareText(TagString, 'SESSIONID') = 0 then
101
if Assigned(Session) then
102
ReplaceText := Session.SessionID;
105
if AnsiCompareText(TagString, 'TIMEOUTMINUTES') = 0 then
107
if Assigned(Session) then
108
ReplaceText := IntToStr(Session.TimeOutMinutes);
111
if AnsiCompareText(TagString, 'SESSIONFILE') = 0 then
113
if Assigned(Session) then
114
ReplaceText := IncludeTrailingPathDelimiter(GetTempDir(True)) + Session.SessionID;
116
used by the session manager returns the OS temporary directory if possible, or from the
117
environment variable TEMP . For CGI programs you need to pass global environment
118
variables, it is not automatic. For example in the Apache httpd.conf with a
119
"PassEnv TEMP" or "SetEnv TEMP /pathtotmpdir" line so the web server passes this
120
global environment variable to the CGI programs' local environment variables.
124
if AnsiCompareText(TagString, 'EXPIREDMESSAGE') = 0 then
126
if Assigned(Session) and ASessionExpired then
127
ReplaceText := TagParams.Values['MESSAGE'];
130
if AnsiCompareText(TagString, 'NEWSESSIONMESSAGE') = 0 then
132
if Assigned(Session) and NewSessionCreated then
133
ReplaceText := TagParams.Values['MESSAGE'];
138
//Not found value for tag -> TagString
139
ReplaceText := '[Template tag {+' + TagString + '+} is not implemented yet.]';
146
RegisterHTTPModule('TFPWebModule1', TFPWebModule1);