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

This chapter discusses:

  • What is a cookie?
  • Sending and receiving cookies.
  • Cookie properties and itemized values.
  • Some other cookie definitions.

What is a Cookie?

Cookie: A small amount of information sent by a Web server to a Web browser, saved by the browser, and sent back to the server later. Cookies are transmitted inside the HTTP header.

Cookies move from server to browser, and back to server as follows:

Web         Web         Local       Web           Web
Server      Browser     System      Browser       Server
Send        Receive     Save        Send back     Receive
cookies --> cookies --> cookies --> cookies   --> cookies

As you can see from the diagram, cookies are actually saved to the hard disk of Web browser user's machines. Many users are concerned about this. But I think it is pretty save to allow your browser to save cookies.

If you are really concerned, you can change your browser's settings to reject cookies. But this may cause many Web based applications fail to run on your browser.

Sending and Receiving Cookies

Sending cookies to the browser can be done by using the response.cookies collection object. You can add new items to this collection easily.

   set c = response.Cookies
   c.Item(name) = value 'Adding or replacing a value with a name.
   c(name) = value 'Same as above - short-hand form
   c.Add value, name 'Same as above
   c.Remove(name) 'Removing a name and its value

Receiving cookies from the browser can be done using the request.Cookies collection object. This is a read-only collection object.

   set c = request.Cookies
   value = c.Item(name) 'Returning the value of the specified item.
   value = c(name) 'Same as above - short-hand form.
   for each n in c 'Iterating by name
      v = c(n)
   next

Here is program to demonstrate how to use those collection objects:

<script language="vbscript" runat="server">
'  cookie_test.asp
'  Copyright (c) 2002 by Dr. Herong Yang
'  This ASP page displays all the cookies received by the server.
'
   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/>")
   next
   response.write("<b>Cookie added by the server:</b><br/>")
   n = "Cookie_" & (c.Count+1)
   v = "Value_" & (c.Count+1)
   response.write( n & " = " & v & "<br/>")
   response.cookies(n) = v
   response.write("</body></html>")
</script>

So I opened this page with IE, I got:

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

(Continued on next part...)

Part:   1  2  3 

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