Sending and Receiving Cookies

This section provides a tutorial example on how to send cookies to the Web browser and receive them back from the Web browser.

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)

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.
   ' 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/>")
   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

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

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.

Last update: 2002.

Table of Contents

 About This Book

 ASP (Active Server Pages) Introduction

 IIS (Internet Information Services) 5.0

 MS Script Debugger

 VBScript Language

 ASP Built-in Run-time Objects

 ASP Session

Creating and Managing Cookies

 What Is a Cookie?

Sending and Receiving Cookies

 Cookie Properties and Itemized Values

 Cookie Test Program Result

 Special Cookies

 Managing Sessions with and without Cookies

 scrrun.dll - Scripting Runtime DLL

 Managing Response Header Lines

 Calculation Speed and Response Time

 ADO (ActiveX Data Object) DLL

 Working with MS Access Database

 Guest Book Application Example


 PDF Printing Version