Setting cookie expiration date.

Got a cookie Q here. I'm trying to set a cookie on a user's machine and give it an expiration date. The cookie gets set fine, nut it seems to expire as soon as the browser is closed. Here's what I'm doing:

   ' Set cookie data
   Response.Cookies("UserPref")("Pref1") = "Test value 1"
   Response.Cookies("UserPref")("Pref2") = "Test value 2"
   Response.Cookies("UserPref")("Pref3") = "Test value 3"

   ' Set expiration date 30 days from today
   Response.Cookies("UserPref").Expires = DateAdd("d", 30, Now())


Later I try to access the cookie, it woks fine unless the session has been canceled & a new one started:

   strVal1 = Request.Cookies("UserPref")("Pref1")
   strVal2 = Request.Cookies("UserPref")("Pref2")
   strVal3 = Request.Cookies("UserPref")("Pref3")

   ' Is the cookie there?
   if strVal1 <> "" and strVal1 <> "" and strVal1 <> "" then
      ...
   End if


Am I setting the expiration incorrectly?
LVL 1
GrailmanAsked:
Who is Participating?
 
KenAdneyConnect With a Mentor Commented:
One of my apps uses a one day cookie.  This works fine for me...

Response.Cookies("my_cookie").Expires = Date + 1
0
 
webdudeCommented:
this is simply a guess, but i think when u set the xpiration of a cookie then it expects a number, not a date. So mebbe try:

Response.Cookies("UserPref").Expires = 30




0
 
mgfranzCommented:
You can set a date in a cookie, Expires is write-only and is the date on which the cookie expires. Unless a date is specified, the cookie will expire when the session ends. If a date is specified, the cookie will be stored on the client's disk after the session ends.

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
mgfranzCommented:
My thinking is the property ExpiresAbsolute needs to be used;

Response.Cookies("UserPref").ExpiresAbsolute = DateAdd("d", 30, Date())

Plus, instead of Now(), use Date().  I don't think the cookie will recognize the time string.


0
 
mgfranzCommented:
As I stated Ken, use Date() instead of Now().
0
 
KenAdneyCommented:
Sorry, mqfranz, I was just reinforcing the idea that the increments are days (vs. hours, months or years)and that I've made it successfully work without ExpiresAbsolute.
0
 
mgfranzCommented:
I agree, Expires() works too.  Absolute is just that...
0
 
webdudeCommented:
as i said earlier...just a guess :)

personally i use session objects, not too many cookies....
0
 
jekl2000Commented:
dim cookiedie
cookiedie=date+365
Response.Cookies("cookiename").Expires=cookiedie

expires in a year

0
 
GrailmanAuthor Commented:
So far I've tried the following with the results indicated in the comments:

Dim CookieExpiresIn
CookieExpiresIn = 30

'     Didn't preserve cookie
Response.Cookies("UserPref").Expires = DateAdd("d", CookieExpiresIn , Now())
Response.Cookies("UserPref").Expires = DateAdd("d", CookieExpiresIn , Date())
Response.Cookies("UserPref").Expires = Date + CookieExpiresIn
Response.Cookies("UserPref").Expires = Date() + 30

'     Microsoft VBScript runtime error '800a01b6'
Response.Cookie("UserPref").ExpiresAbsolute = Date + CookieExpiresIn
Response.Cookie("UserPref").Expires = Date + CookieExpiresIn
Response.Cookies("UserPref").ExpiresAbsolute = Date + CookieExpiresIn
Response.Cookies("UserPref").ExpiresAbsolute = DateAdd("d", CookieExpiresIn , Date())
Response.Cookies("UserPref").ExpiresAbsolute = "July 31, 2002"


I say it didn't preserve the cookie based on the test I indicated in my first post. Is there maybe a problem with the way I'm checking the cookie?
0
 
mgfranzCommented:
Not sure... but to answer your original question, using Date() instead of Now() solved your cookie problem.

Right?

I couldn't find anything relevant to your error message, I would bet that if you wrapped the integer as a string it would work;

CookieExpiresIn = "30"

Or if you converted the integer to a string;

CSrt(CookieExpiresIn)


0
 
GrailmanAuthor Commented:
mgfranz:
As I said in my last post, the cookie didn't get preserved using any of the four statements I listed.

One thing though, it does work using the code from my original post on my machine (Win 2K, IIS). When I upload to my web server though, it acts as though the cookie does not get persisted beyond the end of the session...?
0
 
frueyCommented:
there are some cookie bugs I remember from over a year ago... the user at the time who answered (ClockWatcher) said it's basically better to write cookies RAW and provided an excellent piece of code to do it.

That is, if your webserver is IIS4 on NT4...

...if you're interested maybe you can search cookies and clockwatcher in the archives...
0
 
GrailmanAuthor Commented:
KenAdney:
This worked -
   Response.Cookies("UserPref").Expires = Date + 30

Since it was closest to your code snippet I'll give you the points. The only difference between this and one that I tried earlier (listed in my post) is that I used a constant 30 instead of a variable... I don't know what was going on and why it works now so I'm going to chalk it up to a brain fart. I'll try the variable again after the frustration has died down a bit.

Thanks all for the imput!
Grailman
0
 
mgfranzCommented:
Thanks a lot...

As I stated, your original code was fine, except you needed to use Date instead of Now().

Response.Cookies("UserPref").Expires = DateAdd("d", 30, Date())

Goof.  You should read the comments more closely...


0
 
GrailmanAuthor Commented:
The thing is that I DID read the comments. My post on 08/07/2001 02:56PM showed all the combinations I tried (and yes only the line I was trying at the time was the only one uncommented). Attempts 2 - 4 all use date rather than now which is why I got so frustraited...

Anyhoo, it works now.
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.