Cookies in ASP

A cookie is a small piece of information that a server can store in a Web browser. Each time the browser requests a page from the server it sends the relevant cookies too. ASP allows you to create and retrieve cookies easily.

Creating a Cookie

Cookies are created using Response.Cookies. Cookies are sent in the headers of the response so you should always set cookies before sending the body of the response.

Example Cookie Creation

<%
    Response.Cookies("username") = "jason"
    Response.Cookies("username").Expires = #December 25,2003#
%>

In the example above a cookie named "username" was created with a value of "jason". Then the expiry date was set using the Expires property. If you want to use a relative expiry date, for example, "30 days from now," then set the Expires property to Now() + 30. When the cookie expires it is deleted from the user's Web computer.

Retrieving Cookies

To retrieve cookies we use Request.Cookies.

Example Cookie Retrieval

<%
    username = Request.Cookies("username")
    response.write("Username: " & username)
%>

This code retrieves the value of the cookie named "username" and prints it in the browser. Note that cookies can be retrieved anywhere in the code, unlike creating them which has to be done before anything is written to the body of the response.

Cookies with Multiple Values

Cookies can also contain multiple values, using something called keys. Take a look at this example.

<%
    Response.Cookies("user")("realname") = "Jason Davies"
    Response.Cookies("user")("username") = "jason"
    Response.Cookies("user")("age") = "12"
%>

This is fairly straightforward to understand. Retrieving the cookies is very simple too.

<%
    realname = Response.Cookies("user")("realname")
    username = Response.Cookies("user")("username")
    age = Response.Cookies("user")("age")
%>

Listing All Cookies

You may want to list all the cookies available in a particular request. Here is how to do it.

<%
    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
%>

The HasKeys property is used to check if a cookie has any keys.

The Cookies Collection

Here is a quick reference table of properties that you can set on cookies in ASP.

PropertyDescriptionAccess
Domain Tells the browser only to send the cookie to the specified domain. Write only
Expires Tells the browser to delete the cookie at the specified date. If no date is specified, the cookie expires when the browser is closed. Write only
HasKeys Determines whether the cookie has any keys. Read only
Path Tells the browser only to send the cookie to the specified path. Write only
Secure If set to `True` then the cookie will only be sent to the server when using a secure connection. This defaults to `False`. Write only