ASP - Using Cookies

To createa new cookie, use a statement such as the following:

<%
Response.Cookies("User")="John"
%>

This statement created a new cookie with the name 'User' and set its value to the string 'John'. If a cookie by that name existes already on the client computer, its value is overwritten; if not, a new cookie is created. This cookie is released as soon as the current session ends.

You can specify the expiration date and time with the Expires property of the cookie, as follows:

<%
Response.Cookies("User").Expires = "January 1, 2005 00:00:00 GTM"
%>

To read the cookie from the client computer, use the Cookies property of the Request object and specify the desired cookie by name as following:

<%
username = Request.Cookies("User") 
%>

The 'username' variable holds the string "John" after the execution of this statement.

Cookie Dictionaries

A cookie dictionary has a name like a regular cookie, and its members have names. To access individual members of the dictionary, use another name. To create a dictionary of cookie use the following statement:

<%
Response.Cookies("Category")("Books") = "ASP.NET"
Response.Cookies("Category")("Technology") = "Computers"
Response.Cookies("Category")("Cloths")="White Shirt"
%>

To read the same cookies, use the Cookies collection of the Request object and the same notation:

<%
MyBook = Request.Cookies("Category")("Books")
MyTech = Request.Cookies("Category")("Technology")
MyCloths = Request.Cookies("Category")("Cloths")
%>