How to set and retrieve cookie values?

The Expires property expects to receive a specific data when to expire. For example, the following code will set our user Information cookie to expire on August 1, 2000:


Response.Cookies(“User Information”).Expires = #August 1, 2000#

In VBScript, you see specify a literal date by surrounding it with pound sign (#). There are also many other ways that you could specify the expiration date.

The following list illustrates other methods of setting the Expires property to a fixed date.

· #January 27. 2001#
· “1/27/2001”
· C date (“1/27/2001”)
· Date Serial (2001,1,27)

All the methods listed are valid ways to set the Expires property. However, what if you want the cookie to expire a fixed number of days or weeks from the current date? To do this, use the Date function. The data function allows you to add a number to it to increment or decrement the date value returned. For example, if you want to obtain a data five days in the future, Data + 5 would do the trick. Data - 5 would return a date five days in the past.

This syntax is helpful when setting the Expires property of the collection.
Rarest do you want cookies to expire on a specific date. Far more often you want your cookies to expire a certain number of days, weeks, or months in the future. Let’s say that you wanted to set the cookie MyCookie to expire in a week from the current date:

Response .Cookies (“MyCookies”).Expires = Date + 7

Keep in mind that cookies can only be read by a specified web site. There is a Domain Property that you can set using the Response.Cookies collection. This property, which is equal to your web site’s domain name by default, allows you to enter a different domain name.

The path property also determines how cookies can read. This property can be set to allow cookies to only be read by ASP pages in certain directories. By default , this property is set to your web site’s root directory, which allows cookies created by an ASP page existing in any directory to be read by an ASP page existing in any directory.

It is strongly suggested that you do not change or alter the Domain or path properties. If you leave both properties as the defaults, you will have no trouble reading the cookies you’ve created on your web site. If you set your Domain or path incorrectly, you may not be able to successfully read the cookies you write on your client’s computer. Yet another property is the secure property.

This write-only, Boolean property determines whether a cookie will be sent through a non secure protocol. If this property is set to true, the cookies will only be sent when accessing a pace using the HTTPS protocol. If this property is set to false, its default value, then the cookie will be transmitted over both HTTP and HTTPS.


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.