Response.AddHeader "Set-Cookie" Expires Date

Hello,

I'm having difficulty setting an expiry date on a cookie, using the code below, the cookie is always created with expiry date being 'Session'

Response.AddHeader "Set-Cookie", "Authentication=" & HashValue & "; expires=" & dateAdd("d", 365, Now()) & "; path=/; HttpOnly"

Open in new window


What am i missing?

Thanks
garethtnashAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Try 'Response.Cookies' instead of 'Response.AddHeader'.  More info here:

http://www.w3schools.com/asp/asp_cookies.asp
0
garethtnashAuthor Commented:
Hi Dave,

The only thing is that then I can't set HTTPOnly?

Thanks
0
Dave BaldwinFixer of ProblemsCommented:
I just found out that 'dateAdd' creates the date in the wrong format for a cookie.  The cookie date format looks like this:
expires=Sat, 03 May 2025 17:44:22 GMT
 or
expires=Sat, 03 May 2025 5:44:22 AM
 or
expires=Sat, 03 May 2025 5:44:22 PM

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

garethtnashAuthor Commented:
Thanks for highlighting this Dave, are there any existing vbscript functions, to format the date in the required format?

thanks
0
Dave BaldwinFixer of ProblemsCommented:
I don't know at the moment except that there must be.  This isn't something new.
0
Dave BaldwinFixer of ProblemsCommented:
Here is a demo page with a lot of variations including your code which works fine here.  Remember you can only read the cookie the Second time you load the page, not the first time.
<%
Response.Cookies("firstname")="Alex J Brown"
Response.Cookies("firstname").Expires=#Nov 12,2017#

Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25"
Cexpires = dateAdd("d", 365, Now())
Response.AddHeader "Set-Cookie", "YourCookieName=HTTPCookieValue; path=/; HttpOnly;"
Response.AddHeader "Set-Cookie", "Authentication=SomeHTTPValue; expires=" & dateAdd("d", 365, Now()) & "; path=/; HttpOnly"

                                
%>
<!DOCTYPE html>
<html>
<body>

<%
fname=Request.Cookies("firstname")
response.write("Firstname = " & fname & "<br>" & "<br>")

response.write(CStr("300000") & "<br>")
response.write(CStr(#10-05-25#) & "<br>")
response.write(CStr(Now) & "<br>")

%>
<hr>
<p><b>ASP/VBscript response</b></p>
<%
dim x,y
for each x in Request.Cookies
  response.write("<p>")
  if Request.Cookies(x).HasKeys then
    for each y in Request.Cookies(x)
      response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
      response.write("<br>")
    next
  else
    Response.Write(x & "=" & Request.Cookies(x) & "<br>")
  end if
  response.write "</p>"
next
%>
<hr>
<p><b>Javascript Response</b></p>
<script type="text/javascript">
<!--
function listCookies() {
    var theCookies = document.cookie.split(';');
    var aString = '';
    for (var i = 1 ; i <= theCookies.length; i++) {
        aString += '<br>' + i + ' ' + theCookies[i-1] + "\n";
    }
    return aString;
}

myCookies = listCookies();
document.write(myCookies);
//alert(myCookies);
// -->
</script>
</body>
</html>

Open in new window

0
garethtnashAuthor Commented:
Hi Dave - apologies for the delayed response - this is great thank you
0
Dave BaldwinFixer of ProblemsCommented:
You're welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.