What Is a Cookie?

A "cookie" is a small piece of information which a CGI script can store with a web browser and later read back from that browser.

To create a cookie, an application sends a "Set-Cookie" HTTP header line like this one in response to a URL access from a browser:

    Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
All of these fields except NAME=VALUE are optional.  It is recommended to set the DOMAIN and PATH, as some browsers do not recognize the cookie, if they are not set.

Whenever the browser sends an HTTP request for a URL on a server for which it has stored cookies, it includes a line of the form:

    Cookie: NAME=VALUE; NAME=VALUE; ...
which lists all cookies that apply, which can be read by calling the Web Server/400 Get Environment Variable API.

Examples of Using Cookies

Setting a Cookie

Here is an example of the HTML generated by the store application to set the cookie:
    Content-type: text/html
    Set-Cookie: ITEMS=PROD1,1&PROD2,2; path=/qsys/merchant/;
This sets a cookie that stores that the user selected one of PROD1 and two of PROD2. Since there is no expiration date, the cookie will disappear when the customer closes the browser.

Reading a Cookie

To read the cookie the store calls the Web Server/400 Get Environment Variable API. It passes in an environment variable of "HTTP_COOKIE" which returns the following:

Deleting a Cookie

To delete a cookie, you can set the value of the cookie to blank.  Here is an example of the HTML generated by the store application to clear the cookie after the order has been completed:
    Content-type: text/html
    Set-Cookie: ITEMS=; path=/qsys/merchant/;

Advantages and Disadvantages of Using Cookies

We used cookies in our application to allow us to store what the user has selected to be ordered. We used cookies for the following reasons: The disadvantages of using cookies for this application are: