Custom CGI Application Session Management Example lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- cavimals · 2021-04-01 11:04 I am developing a CGI Application in Lazarus Free Pascal. This is my simple html page with a login page. Code: HTML5  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Test
EmailPassword
I create a New project in Lazarus of Type Custom CGI Application I have the following source code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program cgiproject1; {$mode objfpc}{$H+} uses  Classes, SysUtils, httpDefs, fpweb, custweb, custcgi; Type  TMyCGIHandler = Class(TCgiHandler)  Public    Procedure HandleRequest(ARequest : Trequest; AResponse : TResponse); override;  end;    TMyCGIApp = Class(TCustomCGIApplication)  Protected   function InitializeWebHandler: TWebHandler; override;  end;  Procedure TMyCGIHandler.HandleRequest(ARequest : Trequest; AResponse : TResponse); begin  If (ARequest.ContentFields.Values['email'] = 'xyz@abc.com') AND  (ARequest.ContentFields.Values['password'] = 'hello') Then     begin     AResponse.Contents.Append('Logged in');   {Create Session and Store User Details in Session Variable}     end  else     AResponse.Contents.Append('Incorrect login details');end;  Function TMyCGIApp.InitializeWebHandler: TWebHandler; begin  Result:=TMyCgiHandler.Create(self);end;  begin  With TMyCGIApp.Create(Nil) do    try      Initialize;      Run;    finally      Free;    end;end. Now this code checks login details. If login details are correct it should create a session and store the username in the session. How to implement it. I want to full code including units used, declarations , initiations etc., Thanks ---------------------------------------------------------------------- nummer8 · 2021-04-01 11:28 https://wiki.freepascal.org/fpWeb_Tutorial The fpweb examples have several projects with sessions. In a lazarus installation you can find the examples at .....\lazarus\fpc\3.2.0\source\packages\fcl-web\examples\session and .....\lazarus\fpc\3.2.0\source\packages\fcl-web\examples\routing- session ---------------------------------------------------------------------- cavimals · 2021-04-01 12:50 Hi I saw the codes. I am a beginner in Lazarus. I have been using php only in the past. Could any please edit my code to include the example of Creating New Session and storing the Session Values username. It would be really helpful for me. Thanks In the meanwhile i will try to find my way with the examples. But i doubt i can proccess it. ---------------------------------------------------------------------- cavimals · 2021-04-01 13:01 Hi I change my code to this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program cgiproject1; {$mode objfpc}{$H+} uses  Classes, SysUtils, httpDefs, fpweb, custweb, custcgi, websession, fpHTTP; Type    TSessionModule = class(TFPWebModule)  private    { private declarations }  public    { public declarations }  end;    TMyCGIHandler = Class(TCgiHandler)  Public    Procedure HandleRequest(ARequest : Trequest; AResponse : TResponse); override;  end;    TMyCGIApp = Class(TCustomCGIApplication)  Protected   function InitializeWebHandler: TWebHandler; override;  end; var  SessionModule: TSessionModule; Procedure TMyCGIHandler.HandleRequest(ARequest : Trequest; AResponse : TResponse); begin  If (ARequest.ContentFields.Values['email'] = 'xyz@abc.com') AND  (ARequest.ContentFields.Values['password'] = 'hello') Then     begin     AResponse.Contents.Append('Logged in');     SessionModule.Session.Create(Nil);     SessionModule.Session.Variables['username']:='Vimal';     {Create Session and Store User Details in Session Variable}     end  else     AResponse.Contents.Append('Incorrect login details');end;  Function TMyCGIApp.InitializeWebHandler: TWebHandler; begin  Result:=TMyCgiHandler.Create(self);end;  begin  With TMyCGIApp.Create(Nil) do    try      Initialize;      Run;    finally      Free;    end;end. But i get 500 Internal Server Error. Please help me ---------------------------------------------------------------------- egsuh · 2021-04-02 01:40 Hmmmmm.. I have some experience with CGI module, but not tried CustomCGI application.  I thnk you don't need separate TSessionModule. Instead, my guess is you may define a Session within TMyCGIHandler, and create session during initialization. Anyway session data is transferred via coolkies, which are a TStrings variable of TRequest. Anyway currently your problem seems to lie in that you have not created SessionModule.      begin      AResponse.Contents.Append('Logged in');      SessionModule.Session.Create(Nil);    ////////// <-- Here, SessonModule itself is nil.      SessionModule.Session.Variables['username']:='Vimal';      {Create Session and Store User Details in Session Variable}      end ---------------------------------------------------------------------- cavimals · 2021-04-02 05:10 How to create the session module. Could you please give me the code. ---------------------------------------------------------------------- egsuh · 2021-04-02 05:14 SessionModule:= TSessionModule.Create(nil);