Tuesday, April 30, 2013

Classic ASP Persistent Cookie

I have not really worked with Cookies files. Recently I needed to persist a cookie for a user, however, I noticed that although I would create the cookie it would later disappear. Then I realized that it was disappearing when the browser was closed. I had not realized I was only creating an in-memory cookie that only lasted until the browser was closed.
This is how I was creating the cookie:
intNumber = 1
Response.Cookies("userCookie")=intNumber
However, what I needed it to do was last beyond the browser being closed...be persistent. In order to do that I had to add an expiration like this:
intNumber = 1
startDt=Date
endDate=DateAdd("d",365,startDt)
Response.Cookies("userCookie")=intNumber
Response.Cookies("userCookie").Expires = endDate
And that allowed my cookie file to remain until the user deleted it or it expired!