Solved

Setting cookie expiration date.

Posted on 2001-08-07
16
472 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:Grailman
  • 6
  • 4
  • 2
  • +3
16 Comments
 

Expert Comment

by:webdude
Comment Utility
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
 
LVL 18

Expert Comment

by:mgfranz
Comment Utility
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
 
LVL 18

Expert Comment

by:mgfranz
Comment Utility
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
 
LVL 10

Accepted Solution

by:
KenAdney earned 300 total points
Comment Utility
One of my apps uses a one day cookie.  This works fine for me...

Response.Cookies("my_cookie").Expires = Date + 1
0
 
LVL 18

Expert Comment

by:mgfranz
Comment Utility
As I stated Ken, use Date() instead of Now().
0
 
LVL 10

Expert Comment

by:KenAdney
Comment Utility
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
 
LVL 18

Expert Comment

by:mgfranz
Comment Utility
I agree, Expires() works too.  Absolute is just that...
0
 

Expert Comment

by:webdude
Comment Utility
as i said earlier...just a guess :)

personally i use session objects, not too many cookies....
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:jekl2000
Comment Utility
dim cookiedie
cookiedie=date+365
Response.Cookies("cookiename").Expires=cookiedie

expires in a year

0
 
LVL 1

Author Comment

by:Grailman
Comment Utility
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
 
LVL 18

Expert Comment

by:mgfranz
Comment Utility
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
 
LVL 1

Author Comment

by:Grailman
Comment Utility
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
 
LVL 4

Expert Comment

by:fruey
Comment Utility
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
 
LVL 1

Author Comment

by:Grailman
Comment Utility
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
 
LVL 18

Expert Comment

by:mgfranz
Comment Utility
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
 
LVL 1

Author Comment

by:Grailman
Comment Utility
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now