ASP Cookies Collection

|  | Search W3Schools : |

HOME | HTML | CSS | XML | JAVASCRIPT | ASP | PHP | SQL | MORE... |  | References | Examples | Forum | About

ADVERTISEMENTS XML Certification Download XML editor Custom Programming |  |

Table of contents

ASP Tutorial
ASP HOME
ASP Introduction
ASP Install
ASP Syntax
ASP Variables
ASP Procedures
ASP Forms
ASP Cookies
ASP Session
ASP Application
ASP #include
ASP Global.asa
ASP Send e-mail
ASP Objects
ASP Response
ASP Request
ASP Application
ASP Session
ASP Server
ASP Error
ASP FileSystem
ASP TextStream
ASP Drive
ASP File
ASP Folder
ASP Dictionary
ASP ADO
ASP Components
ASP AdRotator
ASP BrowserCap
ASP Content Linking
ASP Content Rotator
ASP Quick Ref
ASP Summary
Examples/Quiz
ASP Examples
ASP Quiz
ASP Exam

Selected Reading

Web Statistics
Web Glossary
Web Hosting
Web Quality
W3Schools Tutorials
W3Schools Forum
Helping W3Schools

# ASP Cookies Collection

Complete Request Object Reference

The Cookies collection is used to set or get cookie values. If the cookie does not exist, it will be created, and take the value that is specified.

Note: The Response.Cookies command must appear before the <html> tag.

### Syntax

    Response.Cookies(name)[(key)|.attribute]=value

    variablename=Request.Cookies(name)[(key)|.attribute]

Parameter | Description
--- | ---
name | Required. The name of the cookie
value | Required for the Response.Cookies command. The value of the cookie

attribute

Optional. Specifies information about the cookie. Can be one of the following parameters:

- Domain - Write-only. The cookie is sent only to requests to this domain
- Expires - Write-only. The date when the cookie expires. If no date is specified, the cookie will expire when the session ends
- HasKeys - Read-only. Specifies whether the cookie has keys (This is the only attribute that can be used with the Request.Cookies command)
- Path - Write-only. If set, the cookie is sent only to requests to this path. If not set, the application path is used
- Secure - Write-only. Indicates if the cookie is secure

key | Optional. Specifies the key to where the value is assigned

### Examples

The "Response.Cookies" command is used to create a cookie or to set a cookie value:

    <%
    Response.Cookies("firstname")="Alex"
    %>

In the code above, we have created a cookie named "firstname" and assigned the value "Alex" to it.

It is also possible to assign some attributes to a cookie, like setting a date when a cookie should expire:

    <%
    Response.Cookies("firstname")="Alex"
    Response.Cookies("firstname").Expires=#May 10,2002#
    %>

Now the cookie named "firstname" has the value of "Alex", and it will expire from the user's computer at May 10, 2002.

The "Request.Cookies" command is used to get a cookie value.

In the example below, we retrieve the value of the cookie "firstname" and display it on a page:

    <%
    fname=Request.Cookies("firstname")
    response.write("Firstname=" & fname)
    %>

Output:

Firstname=Alex

A cookie can also contain a collection of multiple values. We say that the cookie has Keys.

In the example below, we will create a cookie-collection named "user". The "user" cookie has Keys that contains information about a user:

    <%
    Response.Cookies("user")("firstname")="John"
    Response.Cookies("user")("lastname")="Smith"
    Response.Cookies("user")("country")="Norway"
    Response.Cookies("user")("age")="25"
    %>

The code below reads all the cookies your server has sent to a user. Note that the code checks if a cookie has Keys with the HasKeys property:

    <html>
    <body>

    <%
    dim x,y

    for each x in Request.Cookies
      response.write("<p>")
      if Request.Cookies(x).HasKeys then
        for each y in Request.Cookies(x)
          response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
          response.write("<br /")
        next
      else
        Response.Write(x & "=" & Request.Cookies(x) & "<br />")
      end if
      response.write "</p>"
    next
    %>

    </body>
    </html>
    %>

Output:

firstname=Alex

user:firstname=John
user:lastname=Smith
user: country=Norway
user: age=25

Complete Request Object Reference

## Get Your Diploma!

W3Schools' Online Certification Program is the perfect solution for busy professionals who need to balance work, family, and career building.

The HTML Certificate is for developers who want to document their knowledge of HTML, XHTML, and CSS.

The JavaScript Certificate is for developers who want to document their knowledge of JavaScript and the HTML DOM.

The XML Certificate is for developers who want to document their knowledge of XML, XML DOM and XSLT.

The ASP Certificate is for developers who want to document their knowledge of ASP, SQL, and ADO.

The PHP Certificate is for developers who want to document their knowledge of PHP and SQL (MySQL).

WEB HOSTING
Web based charting for ASP.NET
$15 Domain Name Registration Save $20 / year!
Buy UK Domain Names Register Domain Names
Cheap Domain Names Cheap Web Hosting
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting

WEB BUILDING
Website Templates Flash Templates Website Builder Internet Business Opportunity
Custom Programming
FREE Trial or Demo Web Content Manager Forms,Web Alerts,RSS
Download XML editor
FREE Flash Website
FREE Web Templates

EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification

HOME or Top of Page |  | Printer Friendly
W3Schools is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy. Copyright 1999-2009 by Refsnes Data. All Rights Reserved.