Figure 3 cookieFunctions.inc
<%
'== 1998 William McLuskie, All Rights Reserved
'== Developed exclusively for MIND
Sub cookieWrite(sName, sValue, sExpiration, sPath)
'== Create a cookie with the given attributes.
'== If the attributes are not supplied use defaults.
If sExpiration = "" then
sExpiration = "End of session"
Else
Response.Cookies(sName).Expires = cDate(sExpiration)
End If
If sPath = "" then
sPath = "root"
Else
Response.Cookies(sName).Path = sPath
End If
Response.Cookies(sName) = sValue
If fDebug then
sDebug = sDebug & "<TR><TD>" & sName & "</TD><TD>" & sValue & "</TD><TD>" &
sPath & "</TD><TD>" & sExpiration & "</TD></TR>"
End If
End Sub
Sub cookieWriteDictionary(sName, sKey, sValue, sExpiration, sPath)
'== Create a dictionary cookie entry with the given attributes.
'== If the attributes are not supplied use defaults.
If sExpiration = "" then
sExpiration = "End of session"
Else
Response.Cookies(sName).Expires = cDate(sExpiration)
End If
If sPath = "" then
sPath = "root"
Else
Response.Cookies(sName).Path = sPath
End If
'== create a dictionary entry
Response.Cookies(sName)(sKey) = sValue
If fDebug then
sDebug = sDebug & "<TR><TD>" & sName & "(" & sKey & ")" & "</TD><TD>" &
sValue & "</TD><TD>" & sPath & "</TD><TD>" & sExpiration & "</TD></TR>"
End If
End Sub
Function cookieRead(sName)
'== Return the value for the specified cookie.
cookieRead = Request.Cookies(sName)
End Function
Sub cookieReadAll()
'== Display the values for all available cookies.
Response.Write "<TABLE COLS=2 BORDER=1 CELLPADDING=5>"
Response.Write "<TR ALIGN=CENTER><TD> Name </TD><TD> Value </TD></TR>"
'== Read ALL cookies in a loop
dim vntCookie
dim vntKey
for each vntCookie in Request.Cookies
if Request.Cookies(vntCookie).HasKeys then
for each vntKey in Request.Cookies(vntCookie)
Response.Write "<TR>"
Response.Write "<TD>" & CStr(vntCookie) & "(" & CStr(vntKey) &
") </TD>"
Response.Write "<TD>" & Request.Cookies(vntCookie)(vntKey) & "</TD>"
Response.Write "<TR>"
next
else
Response.Write "<TR>"
Response.Write "<TD>" & CStr(vntCookie) & "</TD>"
Response.Write "<TD>" & Request.Cookies(vntCookie) & "</TD>"
Response.Write "</TR>"
end if
next
Response.Write "</TABLE>"
End Sub
Sub cookieDelete(sName, sPath)
'== Delete the specified cookie.
Dim dtDelete
'== build an expiration date in the past for deletion
dtDelete = DateAdd("d", -1, Now())
Response.Cookies(sName).Expires = cDate(dtDelete)
If sPath = "" then
sPath = "root"
Else
Response.Cookies(sName).Path = sPath
End If
Response.Cookies(sName) = ""
If fDebug then
sDebug = sDebug & "<TR><TD>" & sName & "</TD><TD> </TD><TD>" & sPath & "
</TD><TD> (deleted) </TD></TR>"
End If
End Sub
%>
Figure 4 Response Object Cookie Attributes
Attribute | Description |
Expires | The date and time when the cookie expires. The default is blank. Leaving this blank will create a RAM cookie. |
Domain | Cookies are only returned to pages within the domain from which they were created. ASP will not transmit the cookie if you set the Domain attribute and it does not match the domain the page is running in. Therefore, you should only use the default value. The default value is the domain the page is running in. |
Path | Cookies are only returned to pages within this path. The default is the root directory of the Web application. |
Secure | Determines whether the cookie is secure. This does not encrypt the cookie on the client machine. The default is False. |
Figure 5 CookieWrite.asp
<%@ LANGUAGE="VBSCRIPT" %>
<%
'== 1998 William McLuskie
'== Developed exclusively for MIND
Option Explicit
'== create some local variables
Dim fDebug, sDebug
Dim dtExpire, dtDelete
'== Turn on debugging
fDebug = True
'== build an expiration date for disk cookies
dtExpire = DateAdd("h", 1, Now()) '== Expire in one hour
'== write a few RAM cookies
'== cookieWrite(sName, sValue, sExpires, sPath)
Call cookieWrite("Flavor1", "Apple", "", "")
Call cookieWrite("Flavor2", "Banana", "", "")
Call cookieWrite("Flavor3", "Carrot", "", "/Cookies/Test")
'== write a disk cookie
Call cookieWrite("Flavor4", "Date", dtExpire, "")
'== write a RAM cookie dictionary
'== cookieWriteDictionary(sName, sKey, sValue, sExpires, sPath)
Call cookieWriteDictionary("Ingredients", "Fruit", "Apple", "", "")
Call cookieWriteDictionary("Ingredients", "Flour", "White", "", "")
Call cookieWriteDictionary("Ingredients", "Sugar", "Cane", "", "")
'== display work done
If fDebug then
Response.Write "<TABLE COLS=4 BORDER=1 CELLPADDING=5>"
Response.Write "<TR ALIGN=CENTER><TD> Name </TD><TD> Value </TD><TD>
Path </TD><TD> Expires </TD></TR>"
Response.Write sDebug
Response.Write "</TABLE>"
End If
%>
<!--
#INCLUDE FILE="cookieFunctions.inc"
-->
<HEAD>
</HEAD>
<BODY>
<P>CookieWrite has completed.</P>
</BODY>
</HTML>
Figure 6 CookieWrite Cookies
Name | Path | Value | Expiration |
Flavor1 | root | Apple | End of Session |
Flavor2 | root | Banana | End of Session |
Flavor3 | /Cookies/Test | Carrot | End of Session |
Flavor4 | root | Date | 1 Hour |
Ingredients(Fruit) | root | Apple | End of Session |
Ingredients(Flour) | root | White | End of Session |
Ingredients(Sugar) | root | Cane | End of Session |
Figure 7 CookieRead.asp
<%@ LANGUAGE="VBSCRIPT" %>
<% Option Explicit
'== 1998 William McLuskie
'== Developed exclusively for MIND
'== read all cookies and output
Call CookieReadAll()
%>
<!--
#INCLUDE FILE="cookieFunctions.inc"
-->
<HTML>
<HEAD>
</HEAD>
<BODY>
<P>CookieRead has completed.</P>
</BODY>
</HTML>
Figure 8 Returned CookieWrite Cookies
Name | Value |
Flavor1 | Apple |
Flavor2 | Banana |
Flavor4 | Date |
Ingredients(Fruit) | Apple |
Ingredients(Flour) | White |
Ingredients(Sugar) | Cane |
Figure 9 CookieReadTest Cookies
Name | Value |
Flavor1 | Apple |
Flavor2 | Banana |
Flavor3 | Carrot |
Flavor4 | Date |
Ingredients(Fruit) | Apple |
Ingredients(Flour) | White |
Ingredients(Sugar) | Cane |