Recent

Author Topic: Help needed for usage of session in fpweb  (Read 1616 times)

william

  • Newbie
  • Posts: 6
    • http://pywong.hk.st
Help needed for usage of session in fpweb
« on: May 19, 2022, 10:05:41 am »
Hi all,

I have a bit confused how session work in fpweb. I tried to implement a login page and store the user id in session after a succession log on. However I found that the session ID is being generated upon every request? i.e. always a new session for every web request. I am using embedded server with iniwebsession at the moment for development.

Any idea?

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Help needed for usage of session in fpweb
« Reply #1 on: May 19, 2022, 12:21:15 pm »
Welcome!

The Wiki has a fpWeb Tutorial which includes a section on sessions: see https://wiki.lazarus.freepascal.org/fpWeb_Tutorial#Sessions

william

  • Newbie
  • Posts: 6
    • http://pywong.hk.st
Re: Help needed for usage of session in fpweb
« Reply #2 on: May 19, 2022, 12:55:50 pm »
Following the tutorial I am able to create a simple web application. However I am not sure why a new session will be created on every request. Attached is a very simple project with a single page showing the session id. By refreshing the page http://localhost:8080/TFPWebModule1 I will get new session upon every refresh even using the same tab on the same browser. I suppose I should get a single session until it is expired?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Help needed for usage of session in fpweb
« Reply #3 on: May 22, 2022, 09:51:24 am »
I will get new session upon every refresh even using the same tab on the same browser. I suppose I should get a single session until it is expired?
Can't reproduce. Can you be sure by using developer tools or similar, that FPWebSession cookie is sent back on subsequent requests containing the value returned from the first request?

william

  • Newbie
  • Posts: 6
    • http://pywong.hk.st
Re: Help needed for usage of session in fpweb
« Reply #4 on: May 23, 2022, 03:33:06 am »
Thank your very much for your help. The cookie should be set by inspecting using developer tools. Attached please find a capture for a demo.

william

  • Newbie
  • Posts: 6
    • http://pywong.hk.st
Re: Help needed for usage of session in fpweb
« Reply #5 on: May 26, 2022, 04:48:23 am »
I have spent some time investigating the issue. The session was found to be expired immediately after creation and the issue is located to be error at iniwebsession file.

Code: Pascal  [Select][+][-]
  1. function TIniSessionFactory.SessionExpired(Ini: TMemIniFile): boolean;
  2.  
  3. var
  4.   L: TDateTime;
  5.   T: integer;
  6. begin
  7.   L := Ini.ReadDateTime(SSession, KeyLast, 0);
  8.   T := Ini.ReadInteger(SSession, KeyTimeOut, DefaultTimeOutMinutes);
  9.   {$ifdef cgidebug}
  10.   If (L=0) then
  11.     SendDebug('No datetime in inifile (or not valid datetime : '+Ini.ReadString(SSession,KeyLast,''))
  12.   else
  13.     SendDebug('Last    :'+FormatDateTime('yyyy/mm/dd hh:nn:ss.zzz',L));
  14.   SendDebug('Timeout :'+IntToStr(t));
  15.   {$endif}
  16.   Result := ((Now - L) > (T / (24 * 60)));
  17.   {$ifdef cgidebug}
  18.   if Result then
  19.     begin
  20.     SendDebug('Timeout :'+FloatToStr(T/(24*60)));
  21.     SendDebug('Timeout :'+FormatDateTime('hh:nn:ss.zzz',(T/(24*60))));
  22.     SendDebug('Diff    :'+FormatDateTime('hh:nn:ss.zzz',Now-L));
  23.     SendDebug('Ini file session expired: '+ExtractFileName(Ini.FileName));
  24.     end;
  25.   {$endif}
  26. end;
  27.  

Variable L was found to be zero always since Ini failed to parse the datetime it just wrote (the debugger showed that the correct text was read from the ini file)...

Here is an sample content of the ini file:
Code: [Select]
[Session]
Start=26-May-2022 10:32:04
Timeout=15
Last=26-May-2022 10:32:57

And my system setting of long date format is probably dd-mmmm-yyyy hh:nn:ss. I would suggest the ini session should use a fixed format instead of system setting?

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Help needed for usage of session in fpweb
« Reply #6 on: May 26, 2022, 07:48:58 am »
The cookie should store year-month-day ( TDateTime order) and UTC, since that should be programmer native and only convert for display purposes based on locale settings.
Anything other would be a common design flaw. Storing dates is not complex, but sometimes even advanced programmers fall into that trap.
So if that is the case report a bug.
BUT to my knowledge, fcl-web is correct, so likely you made a programming error where you assume your own locale and that is wrong.

See https://en.wikipedia.org/wiki/Coordinated_Universal_Time

Any software that does not adhere to UTC for date time storage would fail at different time zones.
(If I wasn't officially mad it would drive me nuts)
« Last Edit: May 26, 2022, 08:38:44 am by Thaddy »
Specialize a type, not a var.

william

  • Newbie
  • Posts: 6
    • http://pywong.hk.st
Re: Help needed for usage of session in fpweb
« Reply #7 on: May 27, 2022, 08:29:56 am »
I never change any format setting in the test program and I think there should be a bug somewhere? For example I have created a simple pascal program:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses SysUtils;
  4.  
  5. var
  6.   dt: TDateTime;
  7.   s: string;
  8. begin
  9.   dt := Now;
  10.   s := DateTimeToStr(dt);
  11.   writeln(s);
  12.   writeln(StrToDateTimeDef(s, 0));
  13.   readln();
  14. end.

And this is the output on my PC:
Code: [Select]
27-May-2022 14:28:46
 0.0000000000000000E+000

william

  • Newbie
  • Posts: 6
    • http://pywong.hk.st
Re: Help needed for usage of session in fpweb
« Reply #8 on: May 27, 2022, 09:05:37 am »
Verified to be ShortDateFormat issue, it seems that non-numeric month is causing the troubles. So setting the format to all numeric output fixed the session expired issue, e.g.

Code: Pascal  [Select][+][-]
  1. ShortDateFormat := 'd/m/y';

 

TinyPortal © 2005-2018