PRB: Request.Cookies and Document.cookie Information Do Not Match (262444)



The information in this article applies to:

  • Microsoft Active Server Pages
  • Microsoft Internet Explorer (Programming) 3.0
  • Microsoft Internet Explorer (Programming) 3.01
  • Microsoft Internet Explorer (Programming) 3.02
  • Microsoft Internet Explorer (Programming) 4.0
  • Microsoft Internet Explorer (Programming) 4.01
  • Microsoft Internet Explorer (Programming) 4.01 SP1
  • Microsoft Internet Explorer (Programming) 4.01 SP2
  • Microsoft Internet Explorer (Programming) 5
  • Microsoft Internet Explorer (Programming) 5.01

This article was previously published under Q262444

SYMPTOMS

When you use client-side code (document.cookie) to write cookies, the values written do not match the values that are returned by Request.Cookies in an Active Server Pages page. The most common behavior is that spaces are removed.

For example, if document.cookie is written using the code
<SCRIPT Language=vbscript>
  document.cookie = "MyCookie = Here is some text."
</SCRIPT>
				
the value returned from Request.Cookies("MyCookie") is "Hereissometext.".

CAUSE

Values written to document.cookie are not automatically encoded for HTTP. For example, a colon (:) encoded for HTTP would be returned as "%3A". Request.Cookies expects an encoded string. As a result, the unencoded string is interpreted as encoded, resulting in different values in document.cookie and Request.Cookies.

RESOLUTION

When your code writes a cookie to document.cookie, the code should use the JavaScript escape() function to encode the value for the cookie. The following example writes a cookie called "MyCookie" and encodes the value.
<SCRIPT Language=javascript></SCRIPT>

<SCRIPT Language=vbscript>
  document.cookie = "MyCookie = " & escape("Here is some text.")
</SCRIPT>
				
Note: In order for you to use the JavaScript escape() function from Microsoft Visual Basic Script (VBScript), you must reference the JavaScript scripting engine at least once in the HTML page (as seen in the example).

STATUS

This behavior is by design.

Modification Type:MajorLast Reviewed:10/23/2003
Keywords:kbprb kbScript KB262444