CGICook.pas
What is CGICook?
This program is an example of how to process cookies using Irie Pascal CGI programs. A
full listing of this program is not given on this page, because it would be too long.
However partial program listings are given below, where they may be useful. You can
download the full source code for this program, using the download links at the bottom of
this page.
What does CGICook do?
CGICook displays any cookies passed to it, and displays a form that allows the user to
add, delete, or modify cookies.
What are Cookies?
Cookies are named pieces of information, that a website can send to a browser to be
stored. The browser will send this information back to the website every time the user
returns to the website. Cookies can be stored for days, weeks, or even years, and are
therefore a useful way for websites to "remember" things about visitors.
Setting Cookies
Cookies are set (i.e. created or updated) by sending Set-Cookie:
headers to the browser (apparently these headers should precede the Content-Type:
header). Set-Cookie: headers look like this:
Set-Cookie: name=value; domain=domainname; path=pathinfo;
expires=ExpiryDate; secure
where
name is the name of the cookie and is required.
value is the value to be stored in the cookie.
domain if given specifies the domains to which the cookie will be sent
back to. So for example domain=.irietools.com specifies
that the cookie should be sent back to any website in a domain ending with .irietools.com,
such as www.irietools.com or examples.irietools.com. Notice the leading .
in the domain given, which is required. If domain is not given then the
cookie will only be sent back to the current website.
path if given must be an absolute path and specifies when the browser
should send the cookie back to the website. The URL in the request must match the path
given. If path is not specified then it defaults to the full path of the
current request.
expires if given specifies when the cookie should expire (and be
deleted from the visitor's computer). If expires is not given the cookie
expires when the visitor closes the browser.
secure if given specifies that the cookie should only be returned in
requests made via https.
An example of a Set-Cookie: header is given below.
Set-Cookie: AcceptsCookies=Y; domain=.irietools.com;
path=/cgibin
Reading Cookies
Cookies are returned in the HTTP_COOKIE environment variable, so they
can be read using getenv, like any other CGI environment variable.
How does CGICook work?
The program executes the following statements:
begin
Initialize;
GetCGIData(Cookies);
ProcessCGIData;
GenerateResponse;
Shutdown;
end.
Initialize
This procedure initializes the program.
GetCGIData
This procedure retrieves any form, and cookie data passed to the program. The form data
is stored in a global buffer, and the cookie data is stored in a global list.
ProcessCGIData
This procedure reads the form data in the global buffer (if any is there), and formats
the data into a Set-Cookie header.
GenerateResponse
This procedure generates the response and writes it to the standard output stream. Most
of the HTML in the response is read from a template file (this allows the use of a HTML
editor to maintain most of the HTML in the response). The other HTML, that is specific to
this program consists of:
- A Set-Cookie header that adds, deletes, of modifies a cookie.
- A list of the cookies received from the browser.
- A form that allows the user to add, delete, and modify a cookie.
Shutdown
This procedure cleans up.
Run CGICook
CGICook has been compiled and installed on this server. To see it run, click here.
Download CGICook
Source code: cgicook.pas (size=28,787 bytes)
If you are using the Irie Pascal IDE (available only in the Windows edition) then you
might also want to download the IDE project file: cgicook.ipj
(size=1,765 bytes)
Next > CGI Links |