/packages/fcl-web/examples/session/wmsession.pp

https://github.com/slibre/freepascal · Puppet · 165 lines · 136 code · 29 blank · 0 comment · 1 complexity · 6b9373618b97cec07e337b2fb8494fd5 MD5 · raw file

  1. unit wmsession;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, HTTPDefs, websession, fpHTTP, fpWeb;
  6. type
  7. { TSessionModule }
  8. TSessionModule = class(TFPWebModule)
  9. procedure EndSessionRequest(Sender: TObject; ARequest: TRequest;
  10. AResponse: TResponse; var Handled: Boolean);
  11. procedure InSessionRequest(Sender: TObject; ARequest: TRequest;
  12. AResponse: TResponse; var Handled: Boolean);
  13. procedure NewSessionRequest(Sender: TObject; ARequest: TRequest;
  14. AResponse: TResponse; var Handled: Boolean);
  15. procedure SessionModuleNewSession(Sender: TObject);
  16. private
  17. { private declarations }
  18. public
  19. { public declarations }
  20. end;
  21. var
  22. SessionModule: TSessionModule;
  23. implementation
  24. {$R *.lfm}
  25. { TSessionModule }
  26. {
  27. The default action is the 'InSession' action. When a new session
  28. is started, the newsession event handler is called, and
  29. we set the 'NewSession' action as default.
  30. }
  31. procedure TSessionModule.SessionModuleNewSession(Sender: TObject);
  32. begin
  33. Actions.ActionByName('NewSession').Default:=True;
  34. Actions.ActionByName('InSession').Default:=False;
  35. end;
  36. {
  37. When a new session is detected
  38. - either because there was no session,in which case NewSession is the default
  39. - The URL contained the newsession action in the 'DemoSession' action variable,
  40. something like:
  41. http://localhost/cgi-bin/sessiondemo.cgi?DemoSession=NewSession
  42. in either case, the NewSession action is called, and this event is triggered:
  43. }
  44. procedure TSessionModule.NewSessionRequest(Sender: TObject; ARequest: TRequest;
  45. AResponse: TResponse; var Handled: Boolean);
  46. Var
  47. C : TCookie;
  48. begin
  49. With AResponse.Contents do
  50. begin
  51. Add('<HTML><TITLE>Demo session was started</TITLE><BODY>');
  52. Add('<H1>New session started</H1>');
  53. Add('A new session was started<P>.');
  54. If Session is TFPWebSession then
  55. begin
  56. C:=AResponse.Cookies.FindCookie((Session as TFPWebSession).SessionCookie);
  57. If Assigned(C) then
  58. begin
  59. Add('The issued session cookie is called <B>'+C.Name+'</B><BR> ');
  60. Add('The issued session cookie has value <B>'+C.Value+'</B><BR>.');
  61. end
  62. else
  63. Add('No session cookie was found.');
  64. end;
  65. Add('</BODY></HTML>');
  66. end;
  67. Handled:=True; // Content will be sent.
  68. end;
  69. {
  70. The default action is the 'InSession' action.
  71. We display the session cookie, and the value (name 'Var')
  72. that is currently stored in the session object.
  73. If the user supplied a new value for 'var', we store it in the session.
  74. to supply the value, append
  75. ?var=value
  76. to the URL.
  77. }
  78. procedure TSessionModule.InSessionRequest(Sender: TObject; ARequest: TRequest;
  79. AResponse: TResponse; var Handled: Boolean);
  80. Var
  81. V : string;
  82. C : TCookie;
  83. begin
  84. With AResponse.Contents do
  85. begin
  86. Add('<HTML><TITLE>Demo session active</TITLE><BODY>');
  87. Add('<H1>Demo session active</H1>');
  88. Add('The demo session is still active<P>');
  89. If Session is TFPWebSession then
  90. begin
  91. C:=AResponse.Cookies.FindCookie((Session as TFPWebSession).SessionCookie);
  92. If Assigned(C) then
  93. begin
  94. Add('Current session Cookie is called <B>'+C.Name+'</B><BR>');
  95. Add('and has value <B>'+C.Value+'</B>.');
  96. end;
  97. V:=Session.Variables['Var'];
  98. If (V<>'') then
  99. Add('<P>Stored session value: <B>'+V+'</B>.')
  100. else
  101. Add('<P>No values stored in session.');
  102. V:=ARequest.QueryFields.Values['Var'];
  103. If V<>'' then
  104. begin
  105. Add('<P>Storing new session value: <B>'+V+'</B>.');
  106. Session.Variables['Var']:=V;
  107. end;
  108. end;
  109. Add('</BODY></HTML>');
  110. AResponse.SendContent; // Handles the response.
  111. end;
  112. end;
  113. {
  114. When the 'EndSession' action is called, the session is ended. The
  115. endsession action can be called by providing the 'EndSession' value for
  116. the 'DemoSession' action variable, something like:
  117. http://localhost/cgi-bin/sessiondemo.cgi?DemoSession=EndSession
  118. }
  119. procedure TSessionModule.EndSessionRequest(Sender: TObject; ARequest: TRequest;
  120. AResponse: TResponse; var Handled: Boolean);
  121. begin
  122. // Stop the session
  123. Session.Terminate;
  124. With AResponse.Contents do
  125. begin
  126. Add('<HTML><TITLE>Demo Session Is Terminated</TITLE><BODY>');
  127. Add('<H1>Demo session Terminated</H1>');
  128. Add('The session was terminated, the cookie is cleared and the');
  129. Add('stored value is lost');
  130. Add('</BODY></HTML>');
  131. end;
  132. AResponse.SendContent;
  133. end;
  134. initialization
  135. RegisterHTTPModule('session', TSessionModule);
  136. end.