Link to home
Start Free TrialLog in
Avatar of Grailman
Grailman

asked on

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?
Avatar of webdude
webdude

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




Avatar of Mark Franz
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.

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.


ASKER CERTIFIED SOLUTION
Avatar of KenAdney
KenAdney

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As I stated Ken, use Date() instead of Now().
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.
I agree, Expires() works too.  Absolute is just that...
as i said earlier...just a guess :)

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

expires in a year

Avatar of Grailman

ASKER

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?
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)


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...?
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...
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
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...


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.