Using Cookies

Overview

You can use cookies to store information about a particular client, session, or application. You can then use this information to customize and streamline a client browser's session.

Code Tour

This sample illustrates how your application can query the value of a particular cookie. IIS makes cookies available to ASP scripts through the Request.Cookies collection. This example first queries the cookie CookieVBScript or CookieJScript by using the standard collection-access format, object.collection(keyname). It then resets that cookie to the current date and time.

If the initial query yields a null string, that indicates the client browser has never visited the page in question before, and a first-time welcome message is displayed. If a value is returned by the initial query of CookieVBScript or CookieJScript, however, it indicates not only that the client browser has visited before, but when that last visit took place.

Note   IIS sends all HTTP headers required for a given Web page or script before any HTML is sent to the client browser. Therefore, all statements and methods that modify the HTTP headers of the response, including setting of the Response.Cookies collection members, must be located before the <HTML> tag in your script. If your script attempts to modify the HTTP headers after the server has begun sending HTML content back to the client browser, the script will generate an error.

Location

The VBScript and JScript versions of this script are available in the IIS samples directory, at ...\asp\interaction\Cookie_VBScript.asp and ...\asp\interaction\Cookie_JScript.asp.