Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP Cookies Collection

ASP Cookies Collection


Request Object Reference Complete Request object reference manual


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

**Note:** The Response.Cookies command must appear before the 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 sent only to requests arriving at that domain.
  • Expires - Write-only. The expiration date of the cookie. If no date is specified, the cookie expires when the session ends.
  • HasKeys - Read-only. Specifies whether a cookie has keys (this is the only property that can be used with the Request.Cookies command).
  • Path - Write-only. If set, the cookie is sent only to requests arriving at 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 to set the value for. |

    Example

    The "Response.Cookies" command is used to create a cookie or set the value of a cookie:

    <%

    Response.Cookies("firstname")="Alex"

    %>

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

    You can also set properties for a cookie, such as its expiration time:

    <%

    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.

    The "Request.Cookies" command is used to retrieve cookie values.

    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. We say that such a cookie has keys.

    In the following example, we 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")="Smith"

    Response.Cookies("user")("country")="Norway"

    Response.Cookies("user")("age")="25"

    %>

    The following code can read all the cookies that the server has sent to the user. Note that we use the HasKeys property to determine whether a cookie has keys:

    <%

    dim x,y

    for each x in Request.Cookies

      response.write("

    ")

      if Request.Cookies(x).HasKeys then

        for each y in Request.Cookies(x)

          response.write(x & ":" & y & "=" & Request.Cookies(x)(y))

          response.write("
    ")

        next

      else

        Response.Write(x & "=" & Request.Cookies(x) & "
    ")

      end if

      response.write "

    "

    next

    %>

    %>

    Output:

    firstname=Alex

    user:firstname=John
    user:lastname=Smith
    user:
    country=Norway
    user:
    age=25


    Request Object Reference Complete Request object reference manual