ASP Cookies Collection

response Object Reference Manual

The Cookies collection is used to set or get the value of cookies. If the cookie does not exist, it is created and assigned the specified value.

Note:The Response.Cookies command must be located before the <html> tag.

Syntax:

Response.Cookies(name)[(key)|.attribute]=value
variablename=Request.Cookies(name)[(key)|.attribute]
Parameters Description
name Required. The name of the cookie.
value Required (for the Response.Cookies command). The value of the cookie.
attribute

Optional. Specifies information about the cookie. It can be one of the following parameters.

  • Domain - write-only; the cookie is only sent to requests that reach this domain.
  • Expires - write-only; the expiration date of the cookie. If no date is specified, the cookie will expire at the end of the session.
  • HasKeys - read-only; specifies whether the cookie has keys (this is the only attribute that can be used with the Request.Cookies command)
  • Path - write-only; if set, the cookie is only sent to requests that reach this path. If not set, the application's path is used.
  • Secure - write-only; indicates whether the cookie is secure.
key Optional. Specifies the key where the value is assigned.

Example

"Response.Cookies" command is used to create cookies or set the value of cookies:

<%
Response.Cookies("firstname")="Alex"
%>

In the above code, we created a cookie named "firstname" and assigned it the value alex.

You can also set attributes for cookies, such as setting the expiration time of the cookie:

<%
Response.Cookies("firstname")="Alex" 
Response.Cookies("firstname").Expires=#May 10,2002#
%>

Now, the value of the cookie named "firstname" is "Alex", and its expiration date on the user's computer is May 10, 2002.

"Request.Cookies" command is used to retrieve the value of the cookie.

In the following example, we retrieve the value of the cookie "firstname" and display it on the page:

<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>

Output:

Firstname=Alex

A cookie can contain a collection of multiple values. It is called a cookie with keys.

In the following example, we will create a cookie collection named "user". The "user" cookie has keys that contain information about the user.

<%
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Adams"
Response.Cookies("user")("country")="UK"
Response.Cookies("user")("age")="25"
%>

The following code can read out all the cookies that the server has sent to the user. Please note that we use the HasKeys property to determine if the cookie has keys:

<html>
<body>
<%
dim x,y
for each x in Request.Cookies
  response.write("<p>")
  if Request.Cookies(x).HasKeys then
    for each y in Request.Cookies(x)
      response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
      response.write("<br /")
    next
  else
    Response.Write(x & "=" & Request.Cookies(x) & "<br />")
  end if
  response.write "</p>"
next
%>
</body>
</html>
%>

Output:

firstname=Alex
user:firstname=John
user:lastname=Adams
user:
country=UK
user:
age=25

response Object Reference Manual