3.6 Cookies Collection

Sets or retrieves the value of a collection of cookies.

Cookies derived from the Request object retrieve the values sent by the client browser to the server. The client browser sends the values during an HTTP request.

Cookies derived from the Response object create a new cookie if the specified cookie does not exist. If the cookie exists, it takes the new value and resets the old value.

All the properties are applicable both to the Request and the Response objects.Methods that are applicable only for the Response objects are:

All cookies must be created before the first output is sent to the browser.

3.6.1 Count property

Returns the total number of Cookie objects in the collection.

Syntax

object.Count

Type

Long.

Attributes

Read-only.

3.6.2 HasMoreElements property

Determines whether the specific cookies collection contains more Cookie objects.

Syntax

object.HasMoreElements

Type

Boolean.

Attributes

Read-only.

3.6.3 Add method

Adds a new Cookie object to the Cookies collection.

Syntax

object.Add(
 Name As String,
 Value As String)

Parameters

Name

String. Sets the name of the cookie object.

Value

String. Sets the value of the cookie object.

Return Values

Cookie.

Remarks

This method is applicable only for Response objects. The Cookie value will be included in the HTTP header that is sent to the client.

Example

This sample adds a cookie to the cookie collection and returns the number of cookies in that collection.

<%
On Error Resume Next %>

<% ’Adds a new cookie to the collection
   
  set New = response.cookies.add ("cookie1","users1") 
  if not (New is nothing) then
         new.expires = now + 1
         response.write "Added Cookie1"
      else
    response.write "Did not add Cookie1"
  endif 
Response.write %>

<% ’Returns the number of cookies in the collection
    Set cnt = Response.cookies.count
    Response.write ("The number of cookies in the collection
is: "&cnt) %>

3.6.4 Element method

Returns the specified Cookie object corresponding to the given key value.

Syntax

object.Element(
   Key As AnyType)

Parameters

Key

String/Integer. If the key value is a string it corresponds to the name of a cookie and if it is an Integer it represents the index of the cookie.

Return Values

Cookie object.

3.6.5 GetName Method

Returns the name of the cookie object present in the specific collection.

Syntax

object.GetName

Parameters

None.

Return Values

String.

Remarks

To use this method, the cookie must be available in that collection.

Example

See sample in Value property.

3.6.6 GetValue method

Retrieves the value of the cookie object in the specific cookies collection.

Syntax

object.GetValue

Parameters

None.

Return Values

String.

Remarks

To use this method, the cookie object must be available in the specific cookies collection.

3.6.7 Item method

Retrieves the Cookie object that corresponds to the Key.

Syntax

object.Item(
   Key As AnyType)

Parameters

Key

String/Integer. If the key value is a string it corresponds to the name of a cookie and if it is an Integer it represents the index of the cookie.

Return Values

Cookie.

3.6.8 Next method

Retrieves the next Cookie object from the collection.

Syntax

object.Next()

Parameters

None.

Return Values

Cookie.

3.6.9 Remove method

Removes the specified Cookie object from the collection.

Syntax

object.Remove(
   Key As AnyType)

Parameters

Key

String/Integer. If the key value is a string it corresponds to the name of a cookie and if it is an Integer it represents the index of the cookie.

Return Values

Boolean.

Remarks

This method is applicable only for Response objects. The Cookie value will be included in the HTTP header that is sent to the client.

3.6.10 Reset method

Resets the current object pointer to the beginning of the collection so the next call to Next method retrieves the first cookie in the collection.

Syntax

object.Reset()

Parameters

None.

Return Values

None.