| RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To |
| ISAPI tutorial for Delphi developers | |||||||||||||||||||
| Page 9: Cookies made easy | |||||||||||||||||||
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 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 As you can see, the values of the cookies are stored in the CookieFields property of the request object.
Cookies and re-direction 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.
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). |
|
|
| Got some code to share? Got a question? Need some help? |

