2025-04-27 07:49:33 -04:00

196 lines
6.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-1252">
<TITLE>Cookies</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
TempString = navigator.appVersion
if (navigator.appName == "Microsoft Internet Explorer"){
// Check to see if browser is Microsoft
if (TempString.indexOf ("4.") >= 0){
// Check to see if it is IE 4
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
}
else {
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
}
}
else if (navigator.appName == "Netscape") {
// Check to see if browser is Netscape
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
}
else
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
//-->
</script>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub Window_OnLoad()
Dim frmContents
On Error Resume Next
If Not Parent Is Nothing Then
Set frmContents = Parent.Contents
If Not frmContents Is Nothing Then
frmContents.Window.TOCSynch_Click
End If
End If
End Sub
//--></SCRIPT><META NAME="DESCRIPTION" CONTENT="Internet Information Services reference information">
<META HTTP-EQUIV="PICS-Label" CONTENT='(PICS-1.1 "<http://www.rsac.org/ratingsv01.html>" l comment "RSACi North America Server" by "inet@microsoft.com <mailto:inet@microsoft.com>" r (n 0 s 0 v 0 l 0))'>
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="MS-IT-LOC" Content="Internet Information Services">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H4><A NAME="_cookies_response"></A><SUP></SUP>Cookies</H4>
<P>The <B>Cookies </B>collection sets the value of a cookie. If the specified cookie does not exist, it is created. If the cookie exists, it takes the new value and the old value is discarded.</P>
<H6>Syntax</H6>
<P><B>Response.Cookies(</B><I>cookie</I><B>)</B>[<B>(</B><I>key</I><B>)</B>|<B>.</B><I>attribute</I>]<B> =</B> <I>value</I> </P>
<P>&nbsp;</P>
<H6>Parameters</H6>
<DL>
<DT><I>cookie</I></DT>
<DD>The name of the cookie.<BR>
</DD>
<DT><I>key</I></DT>
<DD>An optional parameter. If <I>key</I> is specified, cookie is a dictionary, and <I>key</I> is set to <I>value</I>.<BR>
</DD>
<DT><I>attribute</I></DT>
<DD>Specifies information about the cookie itself. The attribute parameter can be one of the following.
<TABLE border=0 cellpadding=5 cols=2 frame=box rules=all>
<TR VALIGN="top">
<TD class=blue width=25%><B>Name</B></TD>
<TD class=blue width=75%><B>Description</B></TD>
</TR>
<TR VALIGN="top">
<TD width=25%>Domain</TD>
<TD width=75%>Write-only. If specified, the cookie is sent only to requests to this domain.</TD>
</TR>
<TR VALIGN="top">
<TD width=25%>Expires</TD>
<TD width=75%>Write-only. The date on which the cookie expires. This date must be set in order for the cookie to be stored on the client's disk after the session ends. If this attribute is not set to a date beyond the current date, the cookie will expire when the session ends.</TD>
</TR>
<TR VALIGN="top">
<TD width=25%>HasKeys</TD>
<TD width=75%>Read-only. Specifies whether the cookie contains keys.</TD>
</TR>
<TR VALIGN="top">
<TD width=25%>Path </TD>
<TD width=75%>Write-only. If specified, the cookie is sent only to requests to this path. If this attribute is not set, the application path is used.</TD>
</TR>
<TR VALIGN="top">
<TD width=25%>Secure</TD>
<TD width=75%>Write-only. Specifies whether the cookie is secure.</TD>
</TR>
</TABLE><BR>
<BR>
</DD>
<DT><I>Value</I></DT>
<DD>Specifies the value to assign to <I>key </I>or <I>attribute</I>.</DD>
</DL>
<H6>Remarks</H6>
<P>If a cookie with a key is created, as in the following script,</P>
<PRE><CODE>&lt;%
Response.Cookies("mycookie")("type1") = "sugar"
Response.Cookies("mycookie")("type2") = "ginger snap"
%&gt;
</CODE></PRE>
<P>this header is sent:</P>
<PRE><CODE>Set-Cookie:MYCOOKIE=TYPE1=sugar&amp;TYPE2=ginger+snap
</CODE></PRE>
<P>A subsequent assignment to <CODE>myCookie</CODE><I> </I>without specifying a key, would destroy <CODE>type1</CODE><I> </I>and <CODE>type2</CODE>. This is shown in the following example.</P>
<PRE><CODE>&lt;% Response.Cookies("myCookie") = "chocolate chip" %&gt;
</CODE></PRE>
<P>In the preceding example, the keys <CODE>type1</CODE> and <CODE>type2</CODE> are destroyed and their values are discarded. The <CODE>myCookie</CODE> cookie now has the value <CODE>chocolate chip</CODE>. </P>
<P>Conversely, if you call a cookie with a key, it destroys any non-key values the cookie contained. For example, if after the preceding code you call <B>Response.Cookies</B> with the following</P>
<PRE><CODE>&lt;% Response.Cookies("myCookie")("newType") = "peanut butter" %&gt;
</CODE></PRE>
<P>The value <CODE>chocolate chip</CODE> is discarded and <CODE>newType</CODE> would be set to <CODE>peanut butter</CODE>.</P>
<P>To determine whether a cookie has keys, use the following syntax.</P>
<PRE><CODE>&lt;%= Response.Cookies("myCookie").HasKeys %&gt;
</CODE></PRE>
<P>If <CODE>myCookie</CODE> is a cookie dictionary, the preceding value is TRUE. Otherwise, it is FALSE.</P>
<P>You can use an iterator to set cookie attributes. For example, to set all of the cookies to expire on a particular date, use the following syntax.</P>
<PRE><CODE>&lt;%
For Each cookie in Response.Cookies
Response.Cookie(cookie).Expires = #July 4, 1997#
Next
%&gt;
</CODE></PRE>
<P>You can also iterate through the values of all the cookies in a collection, or all the keys in a cookie. However, if you try to iterate through the values for a cookie that does not have keys, nothing will be returned. To avoid this, you can first use the <B>.HasKeys</B> syntax to check whether a cookie has any keys. This is demonstrated in the following example.</P>
<PRE><CODE>&lt;%
If Not cookie.HasKeys Then
'Set the value of the cookie.
Response.Cookies(cookie) = ""
Else
'Set the value for each key in the cookie collection.
For Each key in Response.Cookies(cookie)
Response.Cookies(cookie)(key) = ""
Next
%&gt;
</CODE></PRE>
<H6>Example</H6>
<P>The following examples demonstrate how you can set a value for a cookie and assign values to its attributes.</P>
<PRE><CODE>&lt;%
Response.Cookies("Type") = "Chocolate Chip"
Response.Cookies("Type").Expires = "July 31, 2001"
Response.Cookies("Type").Path = "/"
%&gt;
</CODE></PRE>
<H6>Applies To</H6>
<P><A HREF="/iishelp/iis/htm/asp/vbob5sj8.htm"><B>Response</B> Object</A></P>
<H6>See Also</H6>
<P><A HREF="/iishelp/iis/htm/asp/vbob0z3o.htm"><B>Request.Cookies</B></A></P>
<hr class="iis" size="1">
<p align="center"><em><a href="../../../common/colegal.htm">&copy; 1997-2001 Microsoft Corporation. All rights reserved.</a></em></p>
</BODY>
</HTML>