March 2, 2021
Hot Topics:

Tracking Users Using WML

  • By Steve Schafer
  • Send Email »
  • More Articles »

How Cookies Work

Cookies work by storing data on the user's local computer/device. This data is stored via an HTTP dialog between the server and the client. That data can then be recalled by the server, processed, updated, etc. Figure 1 shows the data paths associated with storing and retrieving cookies.

Figure 1 - Cookie data is passed back and forth between the server and client via the HTTP stream, but the data is actually stored on the client side.

The information stored in the cookie can be just about any type of data: string, date, an integer, or a real number. Most sites choose to encode cookie data into a lengthy string that can be decoded and parsed by the site code.

Note: Windows users can examine the cookies that have been stored on the local machine. Look in your local settings directory(ies) for "cookies" files. Windows XP users can find the cookies in the following directory:

C:\Documents and Settings\{Username}\Cookies

In addition to data, cookies are stored with a time to live (TTL), specifying how long the cookie should be stored before being discarded. The cookie also indicates the scope for which it should be used—that is, what directory(ies) on the server are valid for that cookie. This allows a site to store multiple cookies with the same name, but different scopes. For example, a site with several sections could store preferences for each section in a cookie named "prefs."

Using Cookies with WML

Because WML doesn't have any built-in cookie functions, you have to use other technologies to store and retrieve cookies. This article shows how to use Perl, which has robust cookie-handling abilities.

We'll use the HTTP header Set-Cookie to set cookies in the examples. Although WML has a <head> tag, don't confuse the HTTP header with the WML card head—they're different animals. Cookies must be set before the end of the HTTP header; the WML <head> comes after the HTTP header has been sent, and therefore it cannot be used to set cookies.

You can use almost any language that supports cookie functions to accomplish the goals in this article. For example, PHP's header() function can be used to set cookies. Even more control can be accomplished with PHP's setcookie() function. Several variable structures exist in PHP to read cookies, including $HTTP_COOKIE_VARS and $_COOKIE arrays.

Handling Cookies with Perl

There are many options for handling cookies in Perl, including simple HTTP methods and even dedicated Perl libraries such as cookie-lib.pl. We'll use the simple HTTP methods for this article.

For more robust cookie management, the reader is encouraged to check out other cookie-handling methods, such as cookie-lib.pl and HTTP::Cookies. The former is available online at The CGI Resource Index (http://cgi.resourceindex.com/Programs_and_Scripts/Perl/Cookies/); the latter is from CPAN (http://search.cpan.org/author/RSE/lcwa-1.0.0/lib/lwp/lib/HTTP/Cookies.pm).

Page 2 of 5

This article was originally published on March 27, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date