ASP   «Prev  Next»

Lesson 9Response.Cookies
ObjectiveStore user-specific information on the client computer.

ASP - User-Specific Information


Response.Cookies(cookiename)[(key)|.attribute] = value 

The key lets you create cookies with keys (nested cookies), as we did for setting user preferences in a previous example:
     .
     .
<TABLE>
<TR>
    <TD>Size</TD>
    <TD>
         <%= Request.Cookies("Preferences")("Size")%>
    </TD>
</TR>
<TR>
    <TD>Neck style</TD>
    <TD>
         <%= Request.Cookies("Preferences")("Neck")%>
    </TD>
</TR>
</TABLE>

You can also specify an attribute, such as an expiration date, for each cookie or key value.

Creating and updating cookies

Creating or updating the cookies we retrieved could be done directly with this code segment:

     .
     .
<%= Response.Cookies("Preferences")("Size") = "XXL"%>
<%= Response.Cookies("Preferences")("Neck") = "17.5"%>
     .
     .

We could also have set the values in the cookies by using data from a submitted HTML FORM in this way:
     .
     .
<%= Response.Cookies("Preferences")("Size") = Request.Form("Size")%>
<%= Response.Cookies("Preferences")("Neck") = Request.Form("Neck")%>
     .
     .

Creating Cookies with and without Keys

In some situations, you may need to create multiple cookies, some with single values and some with keys (nested values). Here is a code segment, again using submitted HTML FORM data, that shows the difference in syntax between the two:

<% Response.Cookies("Name") = Request.Form("Name")%>
<% Response.Cookies("Preferences")("Size") = Request.Form("Size") %>