Working With Cookies in ASP
By Stephen Bucaro
Cookies are small text files that are placed in a visitor's Web browser's
cookie cache by a Web server when someone visits a Web site. By using a cookie the
Web server can store and retrieve information about the visitor between pages.
The cookie may store information about what pages the visitor viewed or what
links the visitor clicked on while they visited the Web site.
In Windows, cookies are stored in:
C:\users\username\AppData\Local\Microsoft\Windows\Temporary Internet Files
You can view the cookie files in Internet Explorer 7 by selecting Internet Options
in the Tools menu and on the General tab, in the Browsing History
section, click on the [Settings] button. In the Temporary Internet Files dialog
box that appears, click on the the [View Files] button. Explorer will open, showing
you the Internet Explorer temporary files, including the cookies.
Some cookies are removed from the visitors computer when they leave the Web
site, these are called session cookies. Other cookies stay in the visitors
browser cookie cache until an expiration date specified by the Web site
that deposited the cookie.
Cookie Reliability
Cookies are generally used to personalize Web sites in order to make the
visitors experience more productive and enjoyable. However, some people don't
want any information about them saved. For this reason some people have
cookies disabled in their browser. A user can delete their Temporary Internet
Files and cookies any time they feel like it. The cookie cache can hold only
a limited number of cookies. When the cache is full and a new cookie arrives,
the oldest cookies are deleted to make room for the new cookie.
Storing a Cookie
Cookies are sent to the visitor's browser using the ASP Response object.
The code for stroring a cookie is shown below.
Response.Cookies("cookiename") = "cookievalue"
Note that the cookie specified above contains no expiration date, this means
it's a session cookie. The code for storing a cookie with an expiration date is
shown below.
Response.Cookies("cookiename") = "cookievalue"
Response.Cookies("cookiename").Expires = "July 10,2012"
Note that the cookie specified above sets an explicit expiration date.
Generally a cookie's expiration date is set by adding a predetermined amount
of time to the current date. The code for this is shown below.
Response.Cookies("cookiename") = "cookievalue"
Response.Cookies("cookiename").Expires = DateAdd("m",1,Date())
The VBScript DateAdd function returns a date to which a specified
amount of time has been added. The "m" in the code above means the time units
to be added will be months ("d" would mean days, "h" would mean hours, "n"
would mean minutes). The 1 in the code above means 1 time unit (in this 1 month)
should be added. The VBScript Date function returns the current date.
Note that if you were specifying an interval of less than 1 day, you would
replace the Date function with the Now) function, which returns
the current date and time.
|