- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


TI3330 - Working with Cookies



The purpose of this document is not to explain what a cookie is or
what it is used for. Instead this document assumes that you are
familiar with the use of cookies and want to know how to work with
them when developing web server applications with Delphi 3.

The TWebRequest and TWebResponse objects provided in Delphi 3 both
have properties which allow for the easy use of cookies. TWebRequest
has the Cookie and CookieFields properties to allow a web server
application to read the cookie header sent as part of a HTTP request.
The TWebResponse object has the Cookies property which allows a web
server application to place a cookie on the client machine through the
cookie header of the HTTP response. This is usually set through the
use of the SetCookieField method.

When a server responds to a HTTP request it sends a document with a
header and a content section. Delphi provides the ability to add a
cookie header through the TWebResponse.Cookies property. This is best
set through the use of the SetCookieField method. The following
TWebActionItem demonstrates the use of the SetCookieField method to
return a cookie to the requesting browser. This example uses
localhost as the domain. You would use this for testing and replace
this string with your domain name. The third paramater is a slash.
This means that this cookie will be sent along with all requests
from the browser while at this domain. For further explainations of
these paramaters see the aforementioned RFC 2109.


procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  MyCookies: TStringList;
begin
  MyCookies := TStringList.Create;
  with MyCookies do begin
    Add('Name=Frank Borland');
    Add('Address=100 Borland Way');
    Add('Product=Delphi');
  end;
  with Response do begin
    SetCookieField(MyCookies, 'localhost', '/', (Now + 1), False);
    Content := 'Cookie planted';
  end;
end;


When a request is made of the HTTP server the client browser issuing
the request will package up all of the aplicable cookie name value
pairs and include them in the HTTP requests cookie header. Delphi 3
will make these available to a web server application in two ways.
The first way is as a string through the Cookie property of the
TWebRequest paramater of a TWebActionItem. It is also available as a
TStrings property called CookieFields. CookieFields is the parsed
contents of the Cookie header of an HTTP request message.

The following are TWebActionItems which extract the name value pairs
and return them to the client in the form of a bare bones HTML page.
The first example returns the cookie as a single string, while the
second example returns each name value pair on a seperate line.


procedure TWebModule1.WebModule1WebActionItem2Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  Response.Content := '' + Request.Cookie +
    '';
end;


procedure TWebModule1.WebModule1WebActionItem3Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  i: integer;
begin
  Response.Content := '';
  for i := 0 to (Request.CookieFields.Count - 1) do
  begin
    Response.Content := Response.Content + '

' + Request.CookieFields[i] + '

'; end; Response.Content := Response.Content + ''; end;



* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile