prev next prev next
  Course navigation Module 3
Lesson 9 Response.Cookies
Objective
Store user-specific information on the client computer.
If there's a cookie with the same name, the cookie takes the new value set by your script. The syntax for setting cookies is:
 
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") %>
    For the first cookie, there are no keys, so just the cookie is given: Name. In the second line, the Preferences cookie has several keys, including Size.
  The next lesson describes how to direct the user to a different Web page.
  Course navigation