Click to return to the Server Technologies home page    
Web Workshop  |  Server Technologies

Simulating Cookies with the Cookie Munger


Microsoft Corporation

October 10, 1997

[Editor's note: We are providing the Cookie Munger on this site in early May 1999.] Download the Cookie Munger Non-MSDN Online link filter.
 

Contents
Introduction
How Cookie Munger Works
Things to Think About When Using Cookie Munger
If You Don't Want to Use Cookie Munger
A Caveat
For More Information

Introduction

In the fast-paced world of Web development, developers who use Active Server Pages (ASP) and session-state methods can easily overlook the fact that not every browser supports cookies. To work around this problem, Microsoft has released the beta version of Cookie Munger. Cookie Munger is an Internet Server Application Programming Interface (ISAPI) filter that munges both the outgoing HTML and incoming HTTP transactions to simulate cookies and maintain session state (or other information stored in cookie text files).

Cookies are a feature of HTML 2.0 and are used by Web developers to enable their sites to save variables or named pieces of information directly on the user's computer. When the user revisits the site, the cookie is sent along, enabling the site to restore variables and state information from the previous session.

Although cookies are an accepted method of tracking of a visitor's session state, many older browsers cannot accept cookies because they pre-date the HTML 2.0 standard (they also lack the ability to parse tables and frames, but that's another story). Web designers aiming for the widest audience possible, however, don't want to leave those browsers out in the cold.

Other users may have disabled their browsers from processing cookies due to privacy concerns. They prefer anonymity, and think that cookies leave a trail of crumbs -- showing where they've been and what their interests are -- on the servers they've visited. (While ASP mostly uses just one cookie, ASPSESSIONID, to maintain session state, ASP application-builders can use additional cookie capabilities to track visitor preferences.)

Still other users have configured their browsers to warn them of incoming cookies, and get very annoyed by the barrage of cookies sent by sites that use ASP. For example, a lot of cookies get sent to a user if there's no GLOBAL.ASA file associated with an ASP application, and a few get sent even when there is. (Nancy Winnick Cluts tells you all about GLOBAL.ASA files and other ASP performance tips in 15 ASP Tips to Improve Performance and Style.)

To effectively use ASP, however, some kind of cookie-ish functionality is necessary. ASP expects a browser to send the ASPSESSIONID cookie with every request it makes. Without it, ASP does not know which session the user owns, and cannot keep the Session object up-to-date as the user moves from page to page. (For a whole article devoted to cookies and session state, check out Michael Levy's ASP and Web Session Management article.) The Cookie Munger filter extends the power and functionality of ASP to users whose browsers cannot or will not accept cookies.

Back to topBack to top

How Cookie Munger Works

The Cookie Munger filter is a utility that enables Active Server Pages to work with browsers that do not support or refuse to accept cookies. The Cookie Munger filter intercepts server requests before they are screened by the ASP filter. Once a request is captured, the filter processes it to determine if the browser is accepting cookies. The process is described as follows:

  1. The server receives a request for a particular URL. Cookie Munger intercepts the request before it is passed to ASP. If the headers of the request contain a "Cookie: ASPSESSIONID=xxx" header (in other words, the browser accepts cookies), then the ASPSESSIONID variable is sent on to ASP to be processed normally. If the URL contains an encoded ASPSESSIONID (a URL that the Cookie Munger filter has added cookie-ish information to), the ASPSESSIONID is removed from the URL, Cookie Munger generates a Cookie header for ASP, and forwards the request.
  2. When ASP sends out a page in response to a request, Cookie Munger intercepts the page before it leaves the server. If Cookie Munger knows the browser accepts cookies, it does nothing. If Cookie Munger instead knows the browser does not accept cookies, it will remove any Set-Cookie headers and munge ASPSESSIONID information onto the end of any local URLs embedded in the page (for example, the URL http://foo/bar.asp will be appended to look something like http://foo/bar.asp-ASP=PVZQGHUMEAYAHMFV).
  3. If the user clicks on one of the modified URLs returned by Cookie Munger, the request comes back to the server, Cookie Munger reconstructs a new ASPSESSIONID header, passes the request on to ASP, and so on.

Back to topBack to top

Things to Think About When Using Cookie Munger

Back to topBack to top

If You Don't Want to Use Cookie Munger

If you elect to not use Cookie Munger, you can minimize the number of cookies sent by Active Server Pages in one of two ways:

      <% Session("something") = whatever %>

Back to topBack to top

A Caveat

The beta version of Cookie Munger is not officially supported by Microsoft Corporation. Peer support is available on the Active Server Pages mailing list or on the microsoft.public.inetserver.iis.activeserverpages newsgroup.

Back to topBack to top

For More Information

To subscribe to the Active Server Pages mailing list, send mail to listserv@listserv.msn. com with

subscribe ActiveServerPages [firstname lastname]

in the body of the message, and follow the directions carefully. (firstname and lastname are optional.)

You can reach the newsgroup through msnews.microsoft.com and other NNTP servers.



Back to topBack to top

Did you find this material useful? Gripes? Compliments? Suggestions for other articles? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.