Recent

Author Topic: Custom CGI Application Session Management Example  (Read 439 times)

cavimals

  • Newbie
  • Posts: 4
Custom CGI Application Session Management Example
« on: April 01, 2021, 01:04:11 pm »
I am developing a CGI Application in Lazarus Free Pascal.

This is my simple html page with a login page.

Code: HTML5  [Select][+][-]
  1. <title>Test</title>
  2. </head>
  3. <form action='http://localhost/cgi-bin/login.cgi' method='post'>
  4. <input type='text' name='email'>Email</input>
  5. <input type='text' name='password'>Password</input>
  6. <input type='submit' value='submit'>
  7. </form>
  8. </body></html>

I create a New project in Lazarus of Type Custom CGI Application I have the following source code

Code: Pascal  [Select][+][-]
  1. program cgiproject1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, SysUtils, httpDefs, fpweb, custweb, custcgi;
  7.  
  8. Type
  9.   TMyCGIHandler = Class(TCgiHandler)
  10.   Public
  11.     Procedure HandleRequest(ARequest : Trequest; AResponse : TResponse); override;
  12.   end;
  13.  
  14.  
  15.   TMyCGIApp = Class(TCustomCGIApplication)
  16.   Protected
  17.    function InitializeWebHandler: TWebHandler; override;
  18.   end;
  19.  
  20.  
  21. Procedure TMyCGIHandler.HandleRequest(ARequest : Trequest; AResponse : TResponse);
  22.  
  23. begin
  24.   If (ARequest.ContentFields.Values['email'] = 'xyz@abc.com') AND  (ARequest.ContentFields.Values['password'] = 'hello') Then
  25.      begin
  26.      AResponse.Contents.Append('Logged in');
  27.      {Create Session and Store User Details in Session Variable}
  28.      end
  29.   else
  30.      AResponse.Contents.Append('Incorrect login details');
  31. end;
  32.  
  33.  
  34. Function TMyCGIApp.InitializeWebHandler: TWebHandler;
  35. begin
  36.   Result:=TMyCgiHandler.Create(self);
  37. end;
  38.  
  39.  
  40. begin
  41.   With TMyCGIApp.Create(Nil) do
  42.     try
  43.       Initialize;
  44.       Run;
  45.     finally
  46.       Free;
  47.     end;
  48. 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

  • Jr. Member
  • **
  • Posts: 70
Re: Custom CGI Application Session Management Example
« Reply #1 on: April 01, 2021, 01:28:01 pm »
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
« Last Edit: April 01, 2021, 01:32:19 pm by nummer8 »

cavimals

  • Newbie
  • Posts: 4
Re: Custom CGI Application Session Management Example
« Reply #2 on: April 01, 2021, 02:50:32 pm »
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

  • Newbie
  • Posts: 4
Re: Custom CGI Application Session Management Example
« Reply #3 on: April 01, 2021, 03:01:33 pm »
Hi
I change my code to this

Code: Pascal  [Select][+][-]
  1. program cgiproject1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, SysUtils, httpDefs, fpweb, custweb, custcgi, websession, fpHTTP;
  7.  
  8. Type
  9.     TSessionModule = class(TFPWebModule)
  10.   private
  11.     { private declarations }
  12.   public
  13.     { public declarations }
  14.   end;
  15.  
  16.  
  17.   TMyCGIHandler = Class(TCgiHandler)
  18.   Public
  19.     Procedure HandleRequest(ARequest : Trequest; AResponse : TResponse); override;
  20.   end;
  21.  
  22.  
  23.   TMyCGIApp = Class(TCustomCGIApplication)
  24.   Protected
  25.    function InitializeWebHandler: TWebHandler; override;
  26.   end;
  27.  
  28. var
  29.   SessionModule: TSessionModule;
  30.  
  31. Procedure TMyCGIHandler.HandleRequest(ARequest : Trequest; AResponse : TResponse);
  32.  
  33. begin
  34.   If (ARequest.ContentFields.Values['email'] = 'xyz@abc.com') AND  (ARequest.ContentFields.Values['password'] = 'hello') Then
  35.      begin
  36.      AResponse.Contents.Append('Logged in');
  37.      SessionModule.Session.Create(Nil);
  38.      SessionModule.Session.Variables['username']:='Vimal';
  39.      {Create Session and Store User Details in Session Variable}
  40.      end
  41.   else
  42.      AResponse.Contents.Append('Incorrect login details');
  43. end;
  44.  
  45.  
  46. Function TMyCGIApp.InitializeWebHandler: TWebHandler;
  47. begin
  48.   Result:=TMyCgiHandler.Create(self);
  49. end;
  50.  
  51.  
  52. begin
  53.   With TMyCGIApp.Create(Nil) do
  54.     try
  55.       Initialize;
  56.       Run;
  57.     finally
  58.       Free;
  59.     end;
  60. end.

But i get 500 Internal Server Error.
Please help me

egsuh

  • Hero Member
  • *****
  • Posts: 683
Re: Custom CGI Application Session Management Example
« Reply #4 on: April 02, 2021, 03:40:06 am »
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

  • Newbie
  • Posts: 4
Re: Custom CGI Application Session Management Example
« Reply #5 on: April 02, 2021, 07:10:19 am »
How to create the session module.
Could you please give me the code.

egsuh

  • Hero Member
  • *****
  • Posts: 683
Re: Custom CGI Application Session Management Example
« Reply #6 on: April 02, 2021, 07:14:55 am »
SessionModule:= TSessionModule.Create(nil);

 

TinyPortal © 2005-2018