Help needed for usage of session in fpweb lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- william · 2022-05-19 08:05 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 · 2022-05-19 10:21 Welcome! The Wiki has a fpWeb Tutorial which includes a section on sessions: see https://wiki.lazarus.freepascal.org/fpWeb_Tutorial#Sessions ---------------------------------------------------------------------- william · 2022-05-19 10:55 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 · 2022-05-22 07:51 Quote from: william on May 19, 2022, 12:55:50 pmI 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 · 2022-05-23 01:33 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 · 2022-05-26 02:48 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TIniSessionFactory.SessionExpired(Ini: TMemIniFile): boolean; var  L: TDateTime;  T: integer;begin  L := Ini.ReadDateTime(SSession, KeyLast, 0);  T := Ini.ReadInteger(SSession, KeyTimeOut, DefaultTimeOutMinutes);  {$ifdef cgidebug}  If (L=0) then    SendDebug('No datetime in inifile (or not valid datetime : '+Ini.ReadString(SSession,KeyLast,''))  else    SendDebug('Last    :'+FormatDateTime('yyyy/mm/dd hh:nn:ss.zzz',L));  SendDebug('Timeout :'+IntToStr(t));  {$endif}  Result := ((Now - L) > (T / (24 * 60)));  {$ifdef cgidebug}  if Result then    begin    SendDebug('Timeout :'+FloatToStr(T/(24*60)));    SendDebug('Timeout :'+FormatDateTime('hh:nn:ss.zzz',(T/(24*60))));    SendDebug('Diff    :'+FormatDateTime('hh:nn:ss.zzz',Now-L));    SendDebug('Ini file session expired: '+ExtractFileName(Ini.FileName));    end;  {$endif}end;  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 · 2022-05-26 05:48 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) ---------------------------------------------------------------------- william · 2022-05-27 06:29 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; uses SysUtils; var  dt: TDateTime;  s: string;begin  dt := Now;  s := DateTimeToStr(dt);  writeln(s);  writeln(StrToDateTimeDef(s, 0));  readln();end. And this is the output on my PC: Code: [Select]27-May-2022 14:28:46  0.0000000000000000E+000 ---------------------------------------------------------------------- william · 2022-05-27 07:05 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ShortDateFormat := 'd/m/y';