Web developers can use cookies in Active Server Pages (ASP) to store and retrieve text-based information on a client browser's computer. Cookies are a great way to persist user information and maintain state with the browser. This article describes how to complete this task.
How to Use the Cookies Collection to Save and Retrieve Data
The following steps demonstrate how to use ASP to store and retrieve information to and from a cookie on the client browser:
From the Windows Start menu, point to Programs, point to Accessories, and then click Notepad.
Highlight the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu.
<%@ Language=VBScript %>
<HTML>
<HEAD>
<%
'Creates a cookie with a string value "Hello World!"
Response.Cookies ("MyCookie")="Hello World!"
%>
</HEAD>
<BODY>
<A HREF="CookieGet.asp">Click to get the cookie value</A>
</BODY>
</HTML>
On the File menu, click Save.
In the Save As dialog box, click the down arrow in the Save In text box, and click the root of your Web server (which is C:\InetPub\Wwwroot by default). In the Save As Type drop-down list box, click All Files. In the File Name text box, type CookieSet.asp. Finally, click Save.
In Notepad, create a second file, and paste the following code:
<%@ Language=VBScript %>
<HTML>
<HEAD>
<%
'Displays the value of the cookie "MyCookie" to the browser.
Response.Write Request.Cookies("MyCookie")
%>
</HEAD>
<BODY>
</BODY>
</HTML>
On the File menu, click Save. Save the file as CookieGet.asp to the same location as the first page.
Start your Web browser, and type the HTTP location of the page in the Address bar to view the page. If you saved the file in the above-mentioned location, type http://<servername>/CookieSet.asp in the Address bar.
To view the value of the cookie, type http://<servername>/CookieGet.asp in the Address bar, or click the link that CookieSet.asp provides.
To add information to the cookie, the sample code uses the Response.Cookies collection. In the preceding sample code, note the following line of code:
Response.Cookies ("MyCookie")="Hello World!"
This code creates a cookie ("MyCookie") and inserts the text "Hello World!".
To retrieve the information from a cookie, the sample code uses the Request.Cookies collection. In the preceding sample code, the following line displays the information that is stored in the cookie to the browser through Response.Write:
Response.Write Request.Cookies("MyCookie")
This example displays the data to the browser, but the data from the cookie can also be stored in a variable or used throughout the ASP page.
displays the values based on the specific key to the browser.
The following table lists all the attributes that you can set by using the Response.Cookies collection:
Collapse this tableExpand this table
Name
Read-only or Write-only
Description
Domain
Write-only
If specified, the cookie is sent only to requests to this domain.
Expires
Write-only
The date on which the cookie expires. This date must be set in order for the cookie to be stored on the client's disk after the session ends. If this attribute is not set to a date beyond the current date, the cookie expires when the session ends.
HasKeys
Read-only
Specifies whether the cookie contains keys.
Path
Write-only
If specified, the cookie is sent only to requests to this path. If this attribute is not set, the application path is used.
Two types of cookies exist: in-memory and disk-based cookies, which are stored on the client's disk. The preceding code sample demonstrates the use of in-memory cookies, which are valid until the browser is closed. To save cookies to the client's disk, the following conditions must be met:
Clients must have cookies enabled in their Web browser.
A valid entry for the Expires attribute must be set. For example, the following code sets this attribute:
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.