ASP Response Object : Cookies Collection

The Cookies collection allows you to set the value of a cookie on the end users machine. If the cookie named already exists, then the new value is assigned and the old value discarded. If the cookie does not exist, then it is created and the value is assigned to it.

There are two mandatory arguments:

There are also two optional arguments:
  • Key - If key is specified then Cookie becomes a VBScript Dictionary object (Scripting.Dictionary object, to be exact), and key is set to value.
  • Attribute - Specifies information about the cookie itself. There are five attributes:
    • Domain - (write-only) Specifies the domain by which this cookie can be accessed (see Request.Cookies for information on accessing cookies).
    • Expires - (write-only) Specifies the date on which the cookie will expire. If a date is not set, then the cookie will expire when the session ends.
    • HasKeys - (read-only) Is a boolean value which specifies whether the cookie contains any keys.
    • Path - (write-only) If specified, the cookie is only sent on requests to this path. If the path is not set, then the application path is used.
    • Secure - (write-only) Indicates that the cookie is secure (should be sent via https).

Cookies provide a way to retain state in the stateless environment of the web. Have you clicked the "Remember me" checkbox while loggin in here at E2? Then you have a cookie on your computer (assuming you're accepting cookies) which has your username and your password (encrypted, of course) in it.


'create a cookie with no keys
  Response.Cookies("myCookie") = "nodefu"
'create a cookie with keys
  Response.Cookies("myCookie2")("nodes") = 42
  Response.Cookies("myCookie2")("votes") = 10
'set some attributes1
  Response.Cookies("myCookie2").Expires = "January 1, 2003"
  Response.Cookies("myCookie2").Domain = ""
If you create a cookie with keys, then assigning a value to the cookie will delete the keys (Example 2a below). Likewise, if you create a cookie without keys and then assign a value to a key, the cookie value will be deleted (Example 2b below).
'Example 2a
  Response.Cookies("myCookie") = "hello world!"
  Response.Cookies("myCookie")("lng") = "English"
'Result: the cookie no longer contains the value "hello world!"
'Example 2b
  Response.Cookies("myCookie2")("lng") = "English"
  Response.Cookies("myCookie2") = "hello world!"
'Result: the cookie no longer has a key named "lng" and no longer contains the value "English"

Back to the ASP Response Object
Back to ASP Objects
1Attributes are always applied to the cookie, not the keys.


All code is my own (and can be used to make a hell of a toaster).

Log in or register to write something here or to contact authors.