1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
ISAPI tutorial for Delphi developers
Page 9: Cookies made easy
 More of this Feature
• Pg 1: Intro to web-broker
• Pg 2: Using this tutorial
• Pg 3: Getting started
• Pg 4: Web Actions
• Pg 5: The first ISAPI app
• Pg 6: TWebRequestObject
• Pg 7: Request-responding
• Pg 8: TPageProducers
• Pg 10: Stateless HTTP
• Pg 11: DB enabled apps
• Pg 12: FAQ
• Pg 13: DB apps - Part 1
• Pg 14: DB apps - Part 2
• Pg 15: DB apps - Part 3
 Join the Discussion
"Post your questions, concerns, views and comments to this article..."
Discuss!
 Related Resources
• Internet programming
• CGI and ISAPI with Delphi

We've all heard of cookies. Some of us love them (specially chocalate-chip ones), and some of us hate them. For the later, skip the rest of this tutorial since I am going to make great use of them. I'm from the school of cookie lovers. I think they are one of the best inventions that have been made (if used correctly of course). In this chapter I am going to give you a simple example of how to set a cookie and how to read one. In the next chapter, I will focus on using cookies to overcome HTTP's stateless nature. In the remaining chapters of this tutorial I will use them very frequently.

I am not going to go into details about the specification of cookies or any technical information. If you want all that stuff check outwww.cookiecentral.com or Netscape's specification at.

   Setting a cookie in Delphi
Cookies are a list of pair values (Name=Value). As such, we can take advantage of Object Pascal's TStringList. This makes cookie managing very easy. Take a look at the example below (from chapter9.dpr).

var slstCookie: TStringList;
begin
 slstCookie := TStringList.Create;
 try
  with slstCookie do begin
   Clear;
   Append('CookieExample=ACookie');
   Append('CookieField1=Value1');
   Append('CookieField2=Value2');
  end;
  Response.SetCookieField
    (slstCookie, '', '/scripts',-1,False);
  Response.Content :=
    'Cookie has been set. Thank you!'; 
 finally
  slstCookie.Free;
 end;
end;

Well that was easy enough. That's all there is to it. First we define a stringlist to store the values of the cookie. Next, we add the field name (NAME) and then the value (VALUE). Finally we call the SetCookieMethod to set the cookie. This method takes 5 parameters. The first one is the stringlist with the cookie information. The second parameter is the domain we are setting the cookie for. If non is specified the current domain will be used. The third parameter is the path (again if non is specified the current path will be used). The fourth parameter indicates the expiry date for the cookie. By setting the value to -1 we will make the cookie disapear once the browser is closed (i.e., it will not be stored on the hard-disk). The last parameter indicates whether the cookie should be set only if a secure connection is available.

As you can see, this is basically the same format as you would use when setting a cookie in any other language.

   Reading the cookie
There would be no use to setting a cookie if we could not later read it. Here's how we can read the cookie:

if Request.CookieFields.Values['CookieField1']<>'' then 
 Response.Content :=
   'The value for CookieField1 is ' +
   Request.CookieFields.Values['CookieField1'] + ''
else
 Response.Content := 'Where is the cookie?';

As you can see, the values of the cookies are stored in the CookieFields property of the request object.

   Cookies and re-direction
Once you start using cookies like hot-cakes, you will start to realise that in some cases you would need to set a cookie and then re-direct the user to another location. One use this would have is to check whether the user accepts cookies in his/her browser. To do this we could set a cookie and the re-direct the response to retrieve the cookie. If the cookie is not present, it would lead us to believe that the user has disactivated cookies. This seems simple enough at first, so let's see what the code would look like:

var slstCookie:TStringList;
begin
 slstCookie := TStringList.Create;
 try
  with slstCookie do begin
   Clear;
   Append('CookieExample=ACookie');
   Append('CookieField1=Value1');
   Append('CookieField2=Value2');
  end;
  Response.SetCookieField
     (slstCookie, '', '/scripts',-1,False);
  Response.SendRedirect
     ('http://localhost/scripts/chapter9.dll/get');
 finally
  slstCookie.Free;
 end;
end;

If you try and run this code, don't be surprised when you don't get the expected result. The problem is that SetCookieField and SendRedirect just don't work together. Period. Forget about it. I don't know whether this is a bug or a *special feature*, but it just won't work.

Now that we know the previous code doesn't work, we need a workaround. We can make use the the HTML meta-tag Refresh for our purposes.

var slstCookie:TStringList;
begin
 slstCookie := TStringList.Create;
 try
  with slstCookie do begin
   Clear;
   Append('CookieExample=ACookie');
   Append('CookieField1=Value1');
   Append('CookieField2=Value2');
  end;
  Response.SetCookieField
    (slstCookie, '', '/scripts',-1,False); 
  Response.Content := '';
 finally
  slstCookie.Free;
 end;
end;

All we are doing is sending back a header that contains the instruction to reload (re-direct) to the URL specified in the 0 seconds specified. I've used this method successfully with all my applications and I'm quite happy with the results. I'm sure there might be some other ways to do this, but for now I'm sticking to this one.

Next page > Overcoming HTTP's Stateless nature > Page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: ADO Cursors - DB/10.
Chapter ten of the free Delphi Database Course for beginners. How ADO uses cursors as a storage and access mechanism, and what you should do to choose the best cursor for your Delphi ADO application.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.