This section provides a tutorial example on how to manage cookie properties and itemized values using the cookie collection object provided in the ASP programming interface.
The cookie collection actually stores cookie objects, not just a named value.
I don't have a full description of the cookie object (class), but I know a number
properties of the cookie object.
respones.cookie(n).Expires: A property to set the expiration date of
this cookie. If the expiration date is set, this cookie will be persisted
to browser's local file system. Date format should be like
"response.cookie(n).Expires = #12/31/2029 00:00:00#". Default is to
expire immediately when the browser closes.
response.cookie(n).Domain: A property to define the domain name.
When the browser is requesting a page from
a server in that domain, it should send this cookie to the server.
response.cookie(n).Path: A property to define the path name. When the browser is requesting a page
within that path name, it should send this cookie to the server.
response.cookie(n).Item(k): A method to add a value as an keyed item into this cookie.
request.cookie(n).Item(k): A method to return a value of an keyed item from this cookie.
request.cookie(n).hasKeys: A property returns true if this cookie has pairs of keys and values.
Here is an ASP page to illustrate these special features:
<script language="vbscript" runat="server">
' special_cookies.asp
' Copyright (c) 2002 by Dr. Herong Yang
' This ASP page sends and receives some special cookies.
'
response.write("<html><body>")
' Displaying all the cookies
response.write("<b>Cookies received at this time:</b>:<br/>")
set c = request.Cookies
response.write("Cookies.Count = " & c.Count & "<br/>")
for each n in c
response.write(n & " = " & c(n) & "<br/>")
if c(n).hasKeys then
for each k in c(n)
response.write(n&".Item("&k&") = " & c(n).Item(k) & "<br/>")
next
end if
next
response.write("<b>Adding a persistent cookie:</b><br/>")
n = "Cookie_" & (c.Count+1)
v = "Value_" & (c.Count+1)
response.write( n & " = " & v & "<br/>")
response.cookies(n) = v
response.cookies(n).expires = #12/31/2029 00:00:00#
response.write("<b>Adding a persistent cookie with domain:</b><br/>")
n = "Cookie_d_" & (c.Count+1)
v = "Value_d_" & (c.Count+1)
response.write(n & " = " & v & "<br/>")
response.cookies(n) = v
response.cookies(n).expires = #12/31/2029 00:00:00#
response.cookies(n).domain = "localhost/"
response.write("<b>Adding a persistent cookie with keys:</b><br/>")
n = "Cookie_p_" & (c.Count+1)
k = "Cookie_c_1_" & (c.Count+1)
v = "Value_c_1_" & (c.Count+1)
response.write(n& ".Item("&k&") = " & v & "<br/>")
response.cookies(n).Item(k) = v
k = "Cookie_c_2_" & (c.Count+1)
v = "Value_c_2_" & (c.Count+1)
response.write(n&".Item("&k&") = " & v & "<br/>")
response.cookies(n)(k) = v
response.cookies(n).expires = #12/31/2029 00:00:00#
response.write("</body></html>")
</script>