|
Synopsis
Response.Cookies(Name)[(Key)|.Attribute] = Value |
We use this collection to create and set cookies to be sent to the client. This collection is the counterpart to
the collection Request.Cookies we use to access and read the values of already-set
cookies (for a complete explanation about cookies, see the HTTP section).
Name is the only mandatory parameter. Value is also mandatory.
Use
Oridnary cookie
We can consider cookies as ordinary variables. Consequently, to set a value to a cookie, we used its name:
|
To set 12 to the cookie "foo", we do:
Response.Cookies("foo") = 12
Subsequently, to get the value of this cookie in another ASP script, we use the collection Request.Cookies:
Request.Cookies("foo")
|
Attributes
One can parameter the cookies with the attributes. It is then possible to set the time out, the validity domain... There
are 5 attributes, they can all be set, except HasKeys, which tells if a cookie has keys or not (see next paragraph).
| attribute |
description |
| Domain |
cookie's domain of validity |
| Expires |
date when the cookie expires. If nothing is specified, the cookie lates till the end of the current session
(if one does not use sessions, the cookie will never expire).
|
| HasKeys |
read only. Tells if the cookie has keys. |
| Path |
validity path of the cookie. By default, it is "/" (i.e. the cookie is valid for the whole site).
|
| Secure |
tells if it is a secure cookie |
Keys
Cookies can also be used as dictionaries (associative arrays); then, some keys are used to access the
different elements of a dictionary:
|
Let's create a "cookie-dictionary" foo has two elements bar1 and bar2:
Response.Cookies("foo")("bar1") = value1
Response.Cookies("foo")("bar2") = value2
Afterwards, we would use this cookie in another ASP page like this (see Request.Cookies):
Request.Cookies("foo")("bar1")
Request.Cookies("foo")("bar2")
|
To know if a newly created cookie behaves like a dictionary (has keys), we use the special attribute HasKeys.
printable format
|