- Irie Pascal
- Web Programming
- Company
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).
You can download the full source code for this program using the download links at the bottom of this page.
CGICook displays any cookies passed to it, and displays a form that allows the user to add, delete, or modify cookies.
Cookies are little pieces of data with names, 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.
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
Cookies are returned in the HTTP_COOKIE environment variable, so they
can be read using getenv
, like any other CGI environment variable.
The main body of the program is shown below (the rest of the program is too long to be shown):
begin
Initialize;
GetCGIData;
ProcessCGIData;
GenerateResponse;
Shutdown;
end.
This procedure initializes the program.
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.
This procedure reads the form data in the global buffer (if any is there), and formats the data into a Set-Cookie header.
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 create most of the response). The other HTML, that is specific to this program consists of:
This procedure cleans up.
CGICook has been compiled and installed on this server. To see it run click cookies.cgi.
Source code: cgicook.pas (size=28,787 bytes).
If you are using the Irie Pascal IDE (available only in the Windows edition) then you should also download the IDE project file: cgicook.ipj (size=1,765 bytes).
Put the source file and the project file in the same directory and open the project file.