This sample uses keys within the cookie to store the data. One cookie is stored on the client, but it contains more than one value. The name, count, text color, and background color values are stored as keys within the cookie.

To see where the cookie and keys stored within it are being set and retrieved, look at the highlighted sections of the ASP source code:

<SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>

    Sub WriteHTMLHeader
        Response.Write "<HTML>"
        Response.Write "<HEAD><TITLE>Cookies Sample</TITLE></HEAD>"
    End Sub

    Sub WriteHTMLFooter
        Response.Write "</BODY>"
        Response.Write "</HTML>"
    End Sub
  
    Sub WriteNamePrompt
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2>"
        Response.Write "Your name (need to enter something here): "
        Response.Write "<INPUT TYPE=TEXT SIZE=40 MAXLENGTH=40 NAME=""name"">"
        Response.Write "</FONT>"
    End Sub

    Sub WriteColorPrompts(textColor)   
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2>"
        Response.Write "<P>"
        Response.Write "<TABLE COLS=2 BORDER=0>"
        Response.Write "<TR VALIGN=TOP><TD NOWRAP>"
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2 COLOR=" & textcolor & ">"
        Response.Write "Select your preferred text color: "
        Response.Write "<TD>"
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2 COLOR=" & textcolor & ">"
        Response.Write "<INPUT TYPE=RADIO NAME=""textcolor"" VALUE=""#000000"">Black"
Response.Write "<BR>"
        Response.Write "<INPUT TYPE=RADIO NAME=""textcolor"" VALUE=""#00008B"">Dark Blue"
Response.Write "<BR>"
        Response.Write "<INPUT TYPE=RADIO NAME=""textcolor"" VALUE=""#DEB887"">Burlywood"
Response.Write "<BR>"
        Response.Write "<INPUT TYPE=RADIO NAME=""textcolor"" VALUE=""#2F4F4F"">Dark Slate Gray"
        Response.Write "</TR></TABLE>"    
        Response.Write "<P>"
        Response.Write "<TABLE COLS=2 BORDER=0>"
        Response.Write "<TR VALIGN=TOP><TD NOWRAP>"
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2 COLOR=" & textcolor & ">"
        Response.Write "<P>Select your preferred background color: "
        Response.Write "<TD>"
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2 COLOR=" & textcolor & ">"
        Response.Write "<INPUT TYPE=RADIO NAME=""bgcolor"" VALUE=""#000000"">Black"
        Response.Write "<BR>"
        Response.Write "<INPUT TYPE=RADIO NAME=""bgcolor"" VALUE=""#00008B"">Dark Blue"
Response.Write "<BR>"
        Response.Write "<INPUT TYPE=RADIO NAME=""bgcolor"" VALUE=""#DEB887"">Burlywood"
Response.Write "<BR>"
        Response.Write "<INPUT TYPE=RADIO NAME=""bgcolor"" VALUE=""#2F4F4F"">Dark Slate Gray"
        Response.Write "</TR></TABLE>"
        Response.Write "</FONT>"
    End Sub

    Sub WriteColorPrompts2(textColor)
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2>"
        Response.Write "<P>"
        Response.Write "<TABLE COLS=2 BORDER=0>"
        Response.Write "<TR VALIGN=TOP><TD NOWRAP>"
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2 COLOR=" & textColor & ">"
        Response.Write "Change your preferred text color: "
        Response.Write "<TD>"
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2 COLOR=" & textcolor & ">"
        Response.Write "<INPUT TYPE=RADIO NAME=""textcolor"" VALUE=""#000000"">Black"
Response.Write "<BR>"
        Response.Write "<INPUT TYPE=RADIO NAME=""textcolor"" VALUE=""#00008B"">Dark Blue"
Response.Write "<BR>"
        Response.Write "<INPUT TYPE=RADIO NAME=""textcolor"" VALUE=""#DEB887"">Burlywood"
Response.Write "<BR>"
        Response.Write "<INPUT TYPE=RADIO NAME=""textcolor"" VALUE=""#2F4F4F"">Dark Slate Gray"
        Response.Write "</TR></TABLE>"    
        Response.Write "<P>"
        Response.Write "<TABLE COLS=2 BORDER=0>"
        Response.Write "<TR VALIGN=TOP><TD NOWRAP>"
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2 COLOR=" & textColor & ">"
        Response.Write "Change your preferred background color: "
        Response.Write "<TD>"
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2 COLOR=" & textcolor & ">"       
        Response.Write "<INPUT TYPE=RADIO NAME=""bgcolor"" VALUE=""#7FFFD4"">Aquamarine"
Response.Write "<BR>"
        Response.Write "<INPUT TYPE=RADIO NAME=""bgcolor"" VALUE=""#F5F5DC"">Beige"
Response.Write "<BR>"
        Response.Write "<INPUT TYPE=RADIO NAME=""bgcolor"" VALUE=""#008000"">Green"
Response.Write "<BR>"
        Response.Write "<INPUT TYPE=RADIO NAME=""bgcolor"" VALUE=""#FFFACD"">Lemon Chiffon"
        Response.Write "</TR></TABLE>"  
        Response.Write "</FONT>"
    End Sub

    Sub WriteColorForm(textColor)
        Response.Write "<P><HR>"
        Response.Write "Feel free to change your preferences at any time:<P>"
        Response.Write "<FORM NAME=""preferences"" METHOD=POST ACTION=""cookies.asp"">" 
        WriteColorPrompts2 textColor
        Response.Write "<P><INPUT TYPE=SUBMIT VALUE=""Save Changes"">"
        Response.Write "</FORM>"
    End Sub

    Sub WriteCompleteForm(textColor)
        WriteHTMLHeader
        Response.Write "<BODY BGCOLOR=#FFFFFF>"
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2>"
        Response.Write "Hi there! Please enter the information below, so this page can be customized for you:"
        Response.Write "</FONT>"
        Response.Write "<FORM METHOD=POST ACTION=""cookies.asp"">" 
        WriteNamePrompt
        WriteColorPrompts textColor
        Response.Write "<P><INPUT TYPE=SUBMIT VALUE=""Submit Form""><INPUT TYPE=RESET VALUE=""Reset Form"">"
        Response.Write "</FORM>"
        WriteHTMLFooter
    End Sub

    Sub DoFirstTimeWelcome(name, count, textColor, bgColor)
        WriteHTMLHeader
        Response.Write "<BODY BGCOLOR=" & bgColor & ">"
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2 COLOR=" & textColor & ">"
        Response.Write "Welcome to this page, " & name & "!"
        WriteColorForm textColor
        WriteHTMLFooter
    End Sub

    Sub DoWelcomeBack(name, count, textColor, bgColor)
        WriteHTMLHeader
        Response.Write "<BODY BGCOLOR=" & bgColor & ">"
        Response.Write "<FONT FACE=Verdana,Arial SIZE=2 COLOR=" & textColor & ">"
        Response.Write "Hi, " & name & "! Welcome back. You have requested this page " & count & " times."
        WriteColorForm textColor
        WriteHTMLFooter
    End Sub


    Sub SetCookieValue(keyName, value)
        ' set the key for the cookie to new value
        ' and set expiration date
        Response.Cookies("wmt")(keyName) = value
        Response.Cookies("wmt").Expires = Date + 365
    End Sub


</SCRIPT>

<%
 
    ' start of main script

    ' get current cookie values
    cookieName = Request.Cookies("wmt")("name")
    cookieCount = Request.Cookies("wmt")("count")
    cookieTextColor = Request.Cookies("wmt")("textcolor")
    cookieBgColor = Request.Cookies("wmt")("bgcolor")

    ' get form inputs
    Set formName = Request.Form("name")
    Set formTextColor = Request.Form("textcolor")
    Set formBgColor = Request.Form("bgcolor")
    Set formRemove = Request.Form("remove")

    ' if cookie has not been set, or form is empty,
    ' prompt for user name and preferences
    If cookieName = "" And formName = "" Then
        WriteCompleteForm ""
    
    ElseIf cookieName = "" Then
    
        ' form is not empty but cookie has not be set, 
        ' so set cookie values
        SetCookieValue "name", formName

        ' if no text color specified, use black
        If formTextColor = "" Then formTextColor = "#000000"
        
        SetCookieValue "textcolor", formTextColor
        
        ' if no background color specified, use white
        If formBgColor = "" Then formBgColor = "#FFFFFF"

        SetCookieValue "bgcolor", formBgColor

        SetCookieValue "count", 2
       
        ' show first time welcome message
        DoFirstTimeWelcome formName, 0, formTextColor, formBgColor
       
    Else
        ' this is a return visitor, so get their preferences
        tempTextColor = cookieTextColor
        tempBgColor = cookieBgColor

        ' if they have changed their text color preference, change corresponding
        ' cookie key
        If formTextColor.Count = 1 Then tempTextColor = formTextColor _ 
            : SetCookieValue "textcolor", formTextColor

        ' if they have changed their background color preference, change 
        ' corresponding cookie key
        If formBgColor.Count = 1 Then tempBgColor = formBgColor _ 
            : SetCookieValue "bgcolor", formBgColor
        
        ' increment count of visits and change its cookie key
        newCount = cookieCount + 1
        SetCookieValue "count", newCount
        
        ' show welcome back message
        DoWelcomeBack cookieName, newCount, tempTextColor, tempBgColor
    
    End If   
%>

© 1999 Microsoft Corporation. All rights reserved. Terms of use.