Cookies are a mechanism by which state can be maintained in a file on the user 's computer. This file is typically stored in a folder named cookies.
NoteAlthough the major Web browsers support cookies, two problems remain :
- Cookies are not a standard feature of HTTP or other specifications. Therefore, they may not be available for all Web browsers.
- Because cookies enable a Web server to create and edit a file on a user's computer (which is a potential security breach), both Netscape and Microsoft enable users to turn off this feature.
Cookies allow information to be associated with a user. You can set and get the values of cookies by using the Cookies collection.
When the Web server returns an HTTP response to a user, the response message may include a cookie. The cookie includes a description of the saved range of URLs for which that cookie is valid.
A cookie is introduced to the user by including a Set-Cookie header as part of an HTTP response. Any HTTP requests made by the user included in the specified range are accompanied by the current value of the cookie.
To set the value of cookies that your Web server sends to a user, you use the Cookies collection of the Response object. If the cookie does not already exist, Response.Cookies will create a new cookie on the user's computer.
Example
This example creates a cookie with the city set to Redmond:<% Response.Cookies("city")="Redmond" %>
If you want the cookie to apply to all of the pages in your Web application, you set the Path attribute of the cookie to "/." The cookie will then be sent by the Web browser during each request for a page in your Web application.
Example
This example sets the Path attribute of the cookie.Response.Cookies("city").Path = "/"
You can set other attributes for cookies, such as when the cookie should expire.
Example
This example sets the cookie's expiration date:Response.Cookies("Type").Expires = "July 31, 1997"
The Web browser sends cookies to the appropriate pages in your Web application. To read the value of a cookie, use the Cookies collection of the Request object.
Example
If the HTTP request sends a cookie with the city set to Redmond, then this example code retrieves the value of Redmond:<%= Request.Cookies("city") %>