How To Use Cookies in an ASP Page (302390)



The information in this article applies to:

  • Microsoft Active Server Pages

This article was previously published under Q302390

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:
  1. From the Windows Start menu, point to Programs, point to Accessories, and then click Notepad.
  2. 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>
    					
  3. On the File menu, click Save.
  4. 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.
  5. 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>
    					
  6. On the File menu, click Save. Save the file as CookieGet.asp to the same location as the first page.
  7. 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.
  8. To view the value of the cookie, type http://<servername>/CookieGet.asp in the Address bar, or click the link that CookieSet.asp provides.
back to the top

Code Explanation

  • 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.
back to the top

More Information

  • 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 ("MyCookie")("Key1")="Hello"
       Response.Cookies ("MyCookie")("Key2")="World"
    						
    To reference the values in a cookie that contains keys, you must use the key value. For example, the code
       Response.Write Request.Cookies("MyCookie")("Key1")
       Response.Write Request.Cookies("MyCookie")("Key2")
    						
    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:
    NameRead-only or Write-onlyDescription
    DomainWrite-onlyIf specified, the cookie is sent only to requests to this domain.
    ExpiresWrite-onlyThe 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.
    HasKeysRead-onlySpecifies whether the cookie contains keys.
    PathWrite-onlyIf specified, the cookie is sent only to requests to this path. If this attribute is not set, the application path is used.
    SecureWrite-onlySpecifies whether the cookie is secure.
back to the top

Troubleshooting

  • 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:
         Response.Cookies("MyCookie").Expires = "12/31/2002"
      							
      NOTE: If the date that is specified for the Expires attribute has already occurred, the cookie is not saved to the client's disk.

back to the top

REFERENCES

For more information about the Cookies collection, see the following Microsoft Web site: For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

175167 How To Store State in Active Server Pages Applications

260971 Description of Cookies


back to the top




Modification Type:MinorLast Reviewed:6/29/2004
Keywords:kbASPObj kbCodeSnippet kbCookie kbhowto kbHOWTOmaster kbScript KB302390 kbAudDeveloper