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
Solved

Setting cookie expiration date.

Posted on 2001-08-07
16
477 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
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




0
 
LVL 18

Expert Comment

by:mgfranz
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.

0
 
LVL 18

Expert Comment

by:mgfranz
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.


0
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

by:
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
0
 
LVL 18

Expert Comment

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

Expert Comment

by:KenAdney
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.
0
 
LVL 18

Expert Comment

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

Expert Comment

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

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

Expert Comment

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

expires in a year

0
 
LVL 1

Author Comment

by:Grailman
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?
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6361787
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
ID: 6362474
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
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...
0
 
LVL 1

Author Comment

by:Grailman
ID: 6366097
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
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...


0
 
LVL 1

Author Comment

by:Grailman
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.
Thanks.
0

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