Reading and writing custom cookies

Last Updated: 9/30/2008



Sections above here:
Home  ยป  Development

Sections below here:

Topics in this section:
Rethinking the User Interface
Writing your first Delphi App
Images and Graphics
Extending Intraweb
Working with COM
Working with ClientDataSets
Creating PDA Applications
Error Handling
Control Size
Reading and writing custom cookies
Miscellaneous
Advanced Development
Session Locking

Search Documentation:

Web applications that have a lot of input fields are often designed for returning users. It is quite convenient to store custom values on the user's web browser, so that the user does not need to re-enter all the required values again and again.

How to store a value on the user's web browser?

The technology used for this requirement are Cookies. Cookies can be sent from the web server to the web browser. The web browser stores them somewhere and remembers what cookie belongs to what web site. A web server can read all cookies that belong to the current website. In fact the browser always send all cookies that belong to the current web site back to the web server with every request.

Note: In the past there have been security issues where web browsers sent back cookies that did not belong to the current web site. This and and the fact that a web server can easily keep track of how often a user returns to a certain web site, cause many people to disable Cookies. Cookies themselves are in no way bad - but they can be abused and they are prone to security flaws - as everything in the modern world.

How to read a cookie with IntraWeb?

You should read cookies when the form is being created. The cookies sent by the browser can be found in the Request object:

procedure TIWForm3.IWAppFormCreate(Sender: TObject);
begin
IWEdit1.Text := WebApplication.Request.CookieFields.Values['IWEdit1'];
end;

This will read the users cookie value of "IWEdit1" when the page is initially created. If the user has cookies disabled or if the cookie has never been set before, then this will set the Text of IWEdit1 to an empty string.

How to write back a cookie with IntraWeb?

To write back the current value of an variable or an IWEdit, as in this example, you should utilize the AfterRender event of the current form. In that event you can collect all values on that page, which are to be stored on the users web browser:

procedure TIWForm3.IWAppFormAfterRender(Sender: TObject);
begin
with WebApplication.Response.Cookies.Add do
begin
Name:='IWEdit1';
Value:=IWEdit1.Text;
end;
end;

You can read/write as many cookies per page as you want, but be aware that setting many or big cookies will have the tradeoff of bigger bandwidth usage, as the cookies are sent back and forth with every single request.

For more information and references about cookies, see here:  

http://en.wikipedia.org/wiki/HTTP_cookie



(C) 2002-2009 - Atozed Software Ltd.