Microsoft KB Archive/302390

= How To Use Cookies in an ASP Page =

Article ID: 302390

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q302390





IN THIS TASK
SUMMARY
 * How to Use the Cookies Collection to Save and Retrieve Data
 * Code Explanation
 * More Information
 * Troubleshooting

REFERENCES



SUMMARY
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.

back to the top

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 %>   <%     'Creates a cookie with a string value &quot;Hello World!&quot; Response.Cookies (&quot;MyCookie&quot;)=&quot;Hello World!&quot; %>    Click to get the cookie value   </li> On the File menu, click Save.</li> 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.</li>  In Notepad, create a second file, and paste the following code: <%@ Language=VBScript %>   <%  'Displays the value of the cookie &quot;MyCookie&quot; to the browser. Response.Write Request.Cookies(&quot;MyCookie&quot;) %>  </HEAD> 

</BODY> </HTML> </li> On the File menu, click Save. Save the file as CookieGet.asp to the same location as the first page.</li> 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.</li> To view the value of the cookie, type http://<servername>/CookieGet.asp in the Address bar, or click the link that CookieSet.asp provides.</li></ol>

back to the top

Code Explanation
<ul>  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 (&quot;MyCookie&quot;)=&quot;Hello World!&quot; This code creates a cookie (&quot;MyCookie&quot;) and inserts the text &quot;Hello World!&quot;. </li>  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(&quot;MyCookie&quot;) 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. </li></ul>

back to the top

More Information
<ul>  Cookie Keys allow you to save multiple values to the same cookie. For example, the following code uses Keys to store multiple values: Response.Cookies (&quot;MyCookie&quot;)(&quot;Key1&quot;)=&quot;Hello&quot; Response.Cookies (&quot;MyCookie&quot;)(&quot;Key2&quot;)=&quot;World&quot; To reference the values in a cookie that contains keys, you must use the key value. For example, the code Response.Write Request.Cookies(&quot;MyCookie&quot;)(&quot;Key1&quot;) Response.Write Request.Cookies(&quot;MyCookie&quot;)(&quot;Key2&quot;) displays the values based on the specific key to the browser. </li> The following table lists all the attributes that you can set by using the Response.Cookies collection: </li></ul>

back to the top

Troubleshooting
<ul> 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: <ul> Clients must have cookies enabled in their Web browser.</li>  A valid entry for the Expires attribute must be set. For example, the following code sets this attribute: Response.Cookies(&quot;MyCookie&quot;).Expires = &quot;12/31/2002&quot; NOTE: If the date that is specified for the Expires attribute has already occurred, the cookie is not saved to the client's disk. </li></ul> </li></ul>

back to the top

<div class="references_section">