Cookies With ASP
page 1 of 1
Published: 25 Sep 2000
Unedited - Community Contributed
Abstract
Cookies can be a good method for passing data between pages and especially for retaining data between sessions. Today, it's pretty safe to assume that anyone who is using your site can use cookies, since nearly every site that is non-static makes use of them(including all ASP sites that use sessions). This article covers how to set and read cookies.
by Steven Smith
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 24189/ 29

Mmmmm, cookies...

Cookies can be a good method for passing data between pages and especially for retaining data between sessions. Today, it's pretty safe to assume that anyone who is using your site can use cookies, since nearly every site that is non-static makes use of them(including all ASP sites that use sessions). It is also possible to set and read cookies using client-side code, but it is a bit more difficult. Reading and writing cookies using Active Server Pages' built in Request and Response objects is incredibly easy.

The cookies collection is a part of the Request and Response objects. They support the standard key-value pairs of all ASP collections, as well as a few additional methods and properties. Note that when setting cookies, you must do so prior to any HTML being sent to the client, because it uses the response object(just like a Response.Redirect, which I'm sure you knew about, didn't you?).  That means put this at the top of your ASP page, before any HTML or Response.Writes.

	Response.Cookies Properties and Methods:
	Response.Cookies("myCookie").Expires = "January 1, 1999"
	Response.Cookies("myCookie")("myValue1") = 1
	Response.Cookies("myCookie")("myValue2") = 2
	Response.Cookies("myCookie")("myValueN") = N
	
	Request.Cookies Properties and Methods:
	Response.Write Request.Cookies("myCookie")("myValue1")
	'(you can check if it has been set with IsNull:)
	If IsNull(Request.Cookies("myCookie")) Then '...

You can create cookies that will expire when the user closes down their browser, if you only need to retain data for a short period of time. Sometimes this is the only feasible way to tie together user state, such as when you cannot use forms or querystrings and you don't want the overhead of a Session variable. An easy way to set the Expires field to a relative time in the future(e.g. 1 week from now) is to use VB's DateAdd() function. It takes three parameters: a string for the unit of time ("d" is days), the number of units(integer), and the starting time(use "Now()"). The cookie set on this page lasts for 1 day using this code:

	Response.Cookies("Stevenator").Expires = dateadd("d",1,now)

One last note. Since the cookies collection does not have to have a series of key/value pairs, it is possible to give the cookie a single value. Be careful with this, because you can overwrite all of the data in your cookie very easily if you do not use the key/value pairs. For example, Response.Cookies("myCookie") = 1 would overwrite all of the data that was set in the example listed up above(1,2,N,etc). A good way to check whether or not the cookie is scalar or a collection is to use the Count method to return how many items it holds.



User Comments

Title: Good, but...   
Name: Old Fogie
Date: 2009-07-08 5:09:51 PM
Comment:
This tells a lot about how to use cookies, but there are some things missing:

1. is "Response.Cookies Properties and Methods:" an executable or a comment?

2. which line actually creates the cookie?

3. how do you create a cookie that deletes itself when the browser closes? That's just what I need, but it doesn't say how to do it, only that it can be done.
Title: Missing some key issues   
Name: Gareth
Date: 2008-10-22 5:42:39 PM
Comment:
You missed two basic but very important tips:

1)How to check if the cookie is set in the first place:

if request.cookies = "" then

2) How to go about deleting the cookies from your own browser whilst you are testing the website locally.
Title: good article   
Name: selva
Date: 2008-09-20 2:51:56 AM
Comment:
very nice explanation to the beginners, thanks a lot
Title: Nice One   
Name: Danyal Butt
Date: 2008-04-25 5:34:59 AM
Comment:
Its a nice effort for very beginner programmers..
Title: cookied   
Name: anup tripathi
Date: 2008-02-28 12:29:18 AM
Comment:
very good job but u should give more about cokie
Title: Good article   
Name: anderander
Date: 2007-11-19 10:32:08 AM
Comment:
Thank you! This article has been very useful for me.
Title: Good   
Name: Clemetn
Date: 2007-08-31 3:55:45 AM
Comment:
Really this article is helpful for me. I was new to cookie. It showed some usefull info about the Cookie. Thanks for the author........
Title: cookies   
Name: May
Date: 2007-07-14 12:39:11 PM
Comment:
Need to be more detail
Title: cookies   
Name: Amii Tiwari
Date: 2007-05-16 1:49:43 AM
Comment:
It's Good Job
Title: cookies   
Name: subrata
Date: 2007-04-28 5:31:28 AM
Comment:
u sent more informetion about cookies
Title: asp tutorial   
Name: pankaj
Date: 2007-04-16 6:06:17 AM
Comment:
u sud make the information more understandable by starting with easy example to hard(depth) sothat all (neophyte to vertran)all can understand and can b benifitted...thanx
Title: For Cookie   
Name: Nalin
Date: 2007-02-01 5:52:17 AM
Comment:
Ur site is very good. U r cover many types of things for using in practical work.
Title: feed back   
Name: Mukesh Kumar Dhiman
Date: 2005-11-14 1:31:46 AM
Comment:
Very Good ,Jhakaas
Title: More info   
Name: Raju
Date: 2005-06-19 12:45:07 PM
Comment:
hi, it would be more helpful if u gave an example on how to read the cookie.....
any suggested webpag....????
Title: will not work all the time   
Name: band
Date: 2005-05-17 4:11:45 AM
Comment:
if the client computer's date/time is set, let's say, a month in the past, your temporary cookies will became persistent cookies in that user. This bug goes for IE.

band.
Title: Good job   
Name: RockY
Date: 2004-12-07 2:14:50 PM
Comment:
Good job. Thanks.
Title: On addition property of cookie commonly overlooked   
Name: Screen Ngenius
Date: 2004-10-20 9:37:41 AM
Comment:
A commonly overlooked property of calling the cookie method is the "Domain" property.

If you've every successfully tested cookies on your test server w/IIS especially and the same script doesn't work on another server, etc. Try using the "Domain" property. This may be especially the case when running IIS in WIndows Server 2000 or a configuration in IIS. I have yet been able to pinpoint it.

THe domain propery must be set with FQDN of your website.
Set this before setting any of the collection properties...


Response.Cookies("Stevenator").Domain = "aspalliance.com"


Try this before dismantaling your functional scripting and using the expensive Session vars.
Title: My cookies dont seem to work   
Name: Martin Abeshaus
Date: 2004-08-30 9:51:02 AM
Comment:
I can't seem to get this to work correctly. I set the cookie to expire in a week, however, whenever I close my browser, the cookies seem to be lost. They are also not showing up in my 'cookie' folder.
Title: Excellent   
Name: HArim Vargas
Date: 2004-08-06 12:04:10 PM
Comment:
Thks, this help a lot,

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-05-28 6:19:25 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search