I'm trying to create a cookie, with the HttpOnly flag enabled.
While there seems to be a plethora of resources about how to do it in Java and .Net, I need to do it in javascript.
Here is my (currently failing) function
createCookie = function(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; domain=my.domain.com; path=/; HttpOnly;";
Thanks -
You cannot access an HttpOnly cookie in JavaScript.
The following quotation is borrowed from the Wikipedia material:
In other words, HttpOnly cookies are made to be used only on the server side.
I wrote an example in PHP:
It alerts
foo=bar
.Remove the cookie, change
$isHttpOnly
totrue
, reload the page, and you'll see an empty alert. But at the same time the browser stores the cookie to send it during a request to the server.