Recent

Author Topic: delete cookies  (Read 431 times)

tintinux

  • Sr. Member
  • ****
  • Posts: 376
    • Gestinux
delete cookies
« on: December 15, 2025, 09:26:01 am »
Hi

Using this code, I successfully set some cookies. I can read them later and they are as well visible in my Firefox inspector.

Code: Pascal  [Select][+][-]
  1. procedure TWebModule.set_cookie(const aResponse: TResponse; const aName, aValue: string);
  2. var
  3.   C: TCookie;
  4. begin
  5.   C := aResponse.Cookies.Add;
  6.   C.Name := aName;
  7.   C.Value := aValue;
  8.   C.Expires := Now + 30;
  9. end;

The issue is when I need to delete them. I tried :

Code: Pascal  [Select][+][-]
  1. procedure TWebModule.delete_cookie(const aResponse: TResponse; const aName: string);
  2. var
  3.   I: Integer;
  4. begin
  5.   for I := 0 to aResponse.Cookies.Count-1 do
  6.     begin
  7.     if aResponse.Cookies[I].Name = aName then
  8.       begin
  9.       aResponse.Cookies.Delete(I);
  10.       exit;
  11.       end;
  12.     end;
  13. end;  

This doesn't delete the named cookie, because aResponse.Cookies.Count is equal to 0 or 1.
There is only the FPWebSession cookie when CreateSession is checked.
But, again, I can read all the cookies I set with
Code: Pascal  [Select][+][-]
  1. aRequest.CookieFields.Values[aName]
  2.  
and show them in my internet explorer.

What am I missing or doing wrong ?

Thanks
 
PS : Of course it is the same with
Code: Pascal  [Select][+][-]
  1. aResponse.Cookies.IndexOfCookie(aName);  
Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: delete cookies
« Reply #1 on: December 15, 2025, 05:14:27 pm »
You should loop over aRequest.CookieFields instead, as aResponse.Cookies is meant for sending cookies as response headers, they're not connected directly unfortunately. That's why aResponse.Cookies is of type the more complex TCookies instead of simply name-value pairs like aRequest.CookieFields. To delete a cookie from browser, don't call Delete(), which is just a TCollection method not overriden to send cookie deletion. It just deletes the cookie from Cookies property. Instead, you should Add the cookie again (aResponse.Cookies.Add), but set the MaxAge property to 0. This is how the standard defines the way to delete a cookie.

tintinux

  • Sr. Member
  • ****
  • Posts: 376
    • Gestinux
Re: delete cookies
« Reply #2 on: December 19, 2025, 08:53:32 am »
Thank you for the tips !
If someone could explain this in the documentation, it would be great.
And the best would be to override the Delete() function (at least to raise an error since it is not to be used)
Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

 

TinyPortal © 2005-2018