| |
|
Wise ASP - How the Cookie Crumbles
Cookies are information stored on the client's machine, and are
sent to the server with each request for information. The server
can set or change the cookie's values, and the Response
object provides an interface for this purpose. Also, the
Request object provides a way to access cookies from the
server.
Why Use Cookies?
Why use cookies at all? We could store the same information in a
database, or file of some other type, on the server's disk. In
fact, it would be more flexible using a database, since it can
handle data other than text.
However, there will be always be an overhead assocaited
with accessing an external data source on the server. Using a cookie
is a very fast and convenient way of storing small amounts of information.
There are limits of course. We shouldn't store more than twenty
cookies for any one domain. One of the most popular uses of cookies
is storing user information. You enter it once and then the server
it many times with future assistance from your side. Let's look
at how we can create cookies on the server. And then we
will look at retrieving them from the server.
Sending Cookies to the Browser
This code adds a cookie to a client's cookie set, or
changes the value of this cookie if it already exists:
Response.Cookies("name") = lastName & firstName
Please note: Any changes to cookies with the
Response object cause HTTP headers to be sent to the client,
and so they must be executed before any HTML is written.
Cookies Properties
Cookies have properties relating to their lifetime
and availability. If we don't set these explicitly, any
information stored in them will be lost when the broswer
is closed. We need to set an expiry date, using the expires
property. We can also set various other properties of a cookie,
to restirct which servers can gain access to information
stored in it. These other properties are domain,
path and secure. Setting the domain to
Request.ServerVariables("Server_name") would mean that only
pages existing on your network would receive the cookie from
the broswer. We can also be more specific by setting the
path to "wsk". The final attribute, secure,
indicates that the cookie should only be transmitted to the
server over a Secure Sockets Layer connection. Here is an
example of using all the properties:
Response.Cookies("name") = lastName
Response.Cookies("name") = firstName
Response.Cookies("name").Domain = Request.ServerVariables("Server_name")
Response.Cookies("name").Expires = "date + 365"
|