Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Setting cookie expiration date.

Posted on 2001-08-07
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?
Question by:Grailman
  • 6
  • 4
  • 2
  • +3

Expert Comment

ID: 6360600
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

LVL 18

Expert Comment

ID: 6360624
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.

LVL 18

Expert Comment

ID: 6360654
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.

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

LVL 10

Accepted Solution

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

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

Expert Comment

ID: 6360928
As I stated Ken, use Date() instead of Now().
LVL 10

Expert Comment

ID: 6361024
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.
LVL 18

Expert Comment

ID: 6361038
I agree, Expires() works too.  Absolute is just that...

Expert Comment

ID: 6361494
as i said earlier...just a guess :)

personally i use session objects, not too many cookies....
LVL 11

Expert Comment

ID: 6361520
dim cookiedie

expires in a year


Author Comment

ID: 6361733
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?
LVL 18

Expert Comment

ID: 6361787
Not sure... but to answer your original question, using Date() instead of Now() solved your cookie problem.


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;



Author Comment

ID: 6362474
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...?

Expert Comment

ID: 6363780
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...

Author Comment

ID: 6366097
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!
LVL 18

Expert Comment

ID: 6366710
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...


Author Comment

ID: 6368289
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.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dreamweaver server behavior gone 6 145
Classic ASP application Will support SQL 2014 5 94
age from date of birth 4 51
IIS components 2 15
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

840 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