Cookies were designed to be used as a mechanism for websites to retain information about the activity the visitor had taken in the past. This is especially common with sites that have shopping carts. Shopping cart information can be stored in a cookie so that when the user returns to the site in the future, the unprocessed shopping cart items would still be available to the user for further processing. As with ASP Sessions, ASP Cookies are used to store information specific to a user of your website. Cookies are stored on the user’s computer during the life of the session or for an extended period of time. If you set the expiration date of the cookie for some day in the future it will remain there until that day unless manually deleted by the user.
ASP Create Cookies
Creating an ASP cookie is very similar to the process of creating an ASP Session variable. As with the Session variable, you must create a name|key/value pair where the key will be the name of the cookie. The cookie will store the value which contains the actual data. The Response.Cookies command is used to create cookies. The Response.Cookies is placed before the openingtag.
Syntax
Properties
In the following example, we will create a cookie named userid and assign the value of john1 to it. The cookie will be set to expire 1 year from the date that it was saved to the user’s computer. When the cookie expires it is deleted from the user’s computer. In this example, we will create a cookie collection named user. The user cookie has Keys that contains information about a user: As you may have noted above, there are a few properties that you can set using cookies. The first property is the Domain property. This is the domain that the cookie originated from. Fortunately, the cookie can only be read by the domain it originated from. It is set by default to the domain in which it was created, but you can modify it according to your needs. The next property in the list is the Expires property. This specifies the date the cookie should expire. If it is not set or set to a past date then it will expire when the browser is closed. The date can be set using different methods. You can use the current date and add or subtract days or set a specific date. The HasKeys property is Read-Only and is used to retrieve cookie information. It is not used when setting specific properties. However, the next item, the Path property is important when using cookies. This specifies in more detail the exact path on the domain that can use the cookie. For example, this would set the path that can retrieve the cookie info. The last property on the list is the Secure property. If set, the cookie will only be set if the browser is using secure sockets (https) to connect.
ASP Retrieve Cookies
The easiest way to retrieve a cookie is to do so by using the Request.Cookies command. If you want to read all the cookies, you can do so using a loop and conditional statement along with the HasKeys property.