ASP Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.11

Using Cookies

Part:   1  2  3 

ASP Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

hyBook - Guestbook Application

Using MS Access Databases

ActiveX Data Object (ADO)

Controlling Response Header Lines

Microsoft Scripting Runtime DLL

Using Cookies

ASP Sessions

ASP Objects

Microsoft Script Debugger

Internet Information Services (IIS)

... Table of Contents

(Continued from previous part...)

Then I clicked the refresh button on the IE window, I got:

Cookies received at this time::
Cookies.Count = 1
Cookie_1 = Value_1
Cookie added by the server:
Cookie_2 = Value_2

What happened here was that when I requested the page the first time, the server received no cookie from the browser's request. But this page added one cookie named as "Cookie_1" to the response. So my browser has one cookie now.

When I clicked the refresh button, my browser requested this page again with this cookie. Now the server received one cookie from the request. But this page added another cookie as "Cookie_2" to the response. So my browser has two cookies now.

If I keep clicking the refresh button, more and more cookies would be added to the request and response. But there is a limit. The browser will only take up to 20 cookies from one Web server.

Cookie Properties and Itemized Values

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>

Request this ASP page, special_cookies.asp, with IE, you will get:

Cookies received at this time::
Cookies.Count = 0
Adding a persistent cookie:
Cookie_1 = Value_1
Adding a persistent cookie with domain:
Cookie_d_2 = Value_d_2
Adding a persistent cookie with keys:
Cookie_p_3.Item(Cookie_c_1_3) = Value_c_1_3
Cookie_p_3.Item(Cookie_c_2_4) = Value_c_2_4

Now click at your IE "Tools" menu and select "Internet Options...". Click the "Settings..." button in the "Temporary Internet files" section of the "General" tab. You will see where is your "Temporary Internet files folder". Go to that folder, you will see a cookie file named something like "Cookie:user@localhost/". Double click on that file, you should be able to open this file in notepad:

(Continued on next part...)

Part:   1  2  3 

Dr. Herong Yang, updated in 2002
ASP Tutorials - Herong's Tutorial Notes - Using Cookies