Please read my blog's comment policy here.
Over the five years I’ve worked on Internet Explorer, I’ve probably seen more questions from the community about HTTP cookies than on any other topic. Cookies are an integral component of most websites in use today, and hence problems or unexpected behaviors with cookies tend to get a lot of attention.
In this post, I’ll try to summarize the most common questions and answers I’ve seen related to Internet Explorer’s implementation of cookies. I may add to this FAQ over time.
Q1: IE’s cookie code doesn’t seem to support <something> as defined in RFC2109 or RFC2965.
A: Correct. Internet Explorer (including IE8) does not attempt to support any RFC for cookies.
WinINET (the network stack below IE) has cookie implementation based on the pre-RFC Netscape draft spec for cookies. This means that directives like max-age, versioned cookies, etc, are not supported in any version of Internet Explorer.
Q2: If I don’t specify a leading dot when setting the DOMAIN attribute, IE doesn’t care?
A: Correct. All current version browsers (Chrome, FF, Opera, etc) seem to treat a leading dot as implicit. Here’s a test case.
Q3: If I don’t specify a DOMAIN attribute when a cookie, IE sends it to all nested subdomains anyway?
A: Yes, a cookie set on example.com will be sent to sub2.sub1.example.com.
Internet Explorer differs from other browsers in this regard. Here’s a test case.
Q4: How many cookies will Internet Explorer maintain for each site?
A: As of August 2007, the per-host limit was increased from 20 to 50. The August update applied to IE5, 6, and 7. IE8 natively can support 50 cookies per host, and Firefox uses the same limit.
It’s worth mentioning that increased cookie limit actually broke the website of a major financial institution. The site depended on cookies beyond the 20 cookie limit getting dropped, and stopped working properly when the limit was increased. This is just one example of how tightly-coupled today’s web is to IE’s cookie implementation. That, in turn, is one reason why the IE team must exercise great care when making any change to IE’s cookie implementation.
The document.cookie property is limited to 10KB.
WinINET will not respect a Set-Cookie response header value longer than 5118 bytes. WinINET allows 50 cookies, each of which is ~5kb long. Hence a request could carry 250kb of cookies. (eek!)
Q5: IE won’t set a cookie when the hostname/domain contains an underscore?
A: Correct. Technically, an underscore (like this _ ) is not a DNS character, and while Windows will let you use an underscore when naming your machine, it warns you that doing so may cause problems. One such problem is that WinINET blocks attempts to set cookies on such domains. See http://support.microsoft.com/kb/316112/en-us
Q6: IE won’t set a cookie for certain domains, like those of the format http://xx.yy?
A: Correct. The idea is that you may not set a cookie on a "top-level" domain shared by unrelated organizations. Historically, ccTLDs of the format xx.yy were effective TLDs, so cookies may not be set on them. While this heuristic was never perfect, it's been unchanged for over 15 years and hence is not likely to change any time soon. The intricacy of this issue merits a long blog post all its own-- see this post.
Q7: My site is not receiving cookies when it is running in an IFRAME and the parent page is from a different domain. Why?
A: Internet Explorer has restrictions on “3rd party” cookies. 3rd party cookies are cookies which are set or sent for resources from a different domain than the top-level browsing context. You can easily confirm P3P/Cookie restrictions as the root cause of such issues by temporarily changing IE’s Tools / Options / Privacy setting to “Accept All Cookies”.
In order to allow such cookies to be sent reliably, you should send a P3P header when setting the cookie.
You can use Fiddler’s “Privacy Inspector” to view and analyze any P3P Policy. To learn more about P3P and IE, see my quick P3P guide.
Q7b: My page doesn't receive or set cookies when it is running in an IFRAME with the SECURITY=RESTRICTED attribute. Why?
A: The SECURITY=RESTRICTED attribute on IFRAMEs indicates that the browser should treat the content as if it came from the Restricted Sites zone. Content in this zone cannot set or read cookies. Cookies will not be sent to the server, and cookies will not be set if received from the server. To resolve this problem, you will need to communicate any necessary state information to the server via another mechanism (e.g. a token in the URL).
Q8: Are there any limits to the HTML DOM document.cookie property?
A: In IE5, 6, and 7, if the cookie string is longer than 4096 bytes, the document.cookie property will return an empty string. For IE8, this limit was increased to 10Kb.
Also, due to an obscure bug in the underlying WinINET InternetGetCookie implementation, IE’s document.cookie will not return a cookie if it was set with a path attribute containing a filename.
For instance, if a page sets a cookie on itself like so:
…the cookie will be sent with HTTP requests but will not appear in the document.cookie collection.
Q9: Cross-Site Scripting attacks (XSS) can steal cookies. What can I do?
A: Determine if you need to expose your cookies to scripts running on your site. If your cookies are only used by your server, and your scripts don’t require access to your cookies, use the HttpOnly attribute to help protect your site against cookie theft via cross-site scripting attacks.
Simply add the HttpOnly attribute to each Set-Cookie header, and Internet Explorer will ensure that your cookie is not available to any script running in your pages. Cookies with the HttpOnly attribute are still sent in each HTTP request, but will not appear in the script-accessible document.cookies property. This means that if a hacker finds a cross-site scripting hole in your site, he cannot easily use the hole to steal logged-on visitors’ cookies.
The HttpOnly attribute is supported in all modern browsers (IE6+, FF3+, Safari 4, Chrome, Opera 9.5+).
Q10: How can applications or native code add-ons set cookies?
A: Applications should set cookies using the InternetSetCookieEx function, passing the appropriate flags to indicate if a cookie is being set from a 3rd party context, and any P3P directives available. The non-EX version of this function will unconditionally set a cookie (even if “Block all cookies” is set in IE’s settings) although such cookies will not be subsequently sent to servers while the “Block all cookies” setting is active.
Note: On Windows Vista and above, Internet Explorer runs Internet content in Protected Mode, a sandbox with an isolated cookie store. In order to set a cookie in the Protected Mode sandbox from an external application running at Medium integrity (aka outside of Internet Explorer), you must use the IESetProtectedModeCookie function. This API was added in IE8, and unfortunately, there is no straightforward alternative for IE7. This API has a number of limits, in particular, it cannot be called by processes running at High Integrity (Admin), and it does not have an option to provide the P3P policy string when setting the cookie.
IE10+ on Windows 8+ introduced Enhanced Protected Mode which uses AppContainers (rather than Integrity Levels) for isolation. EPM does not offer an API for interacting with cookies; IESetProtectedModeCookie will not set the cookie inside an AppContainer.
Q11: How can applications or native code add-ons retrieve cookies?
A: Use the InternetGetCookieEx function.
Note, by default, the cookies returned from this function will not include any HTTPOnly cookies. To retrieve HTTPOnly cookies, you must pass the INTERNET_COOKIE_HTTPONLY flag, available in IE8+. If you decide to pass this flag, you must ensure that your code will not expose the returned value to any script-controllable context. (Note: It appears that support for the INTERNET_COOKIE_HTTPONLY flag was added to IE7 in a cumulative update (KB960818)).
Note: On Windows Vista and above, Internet Explorer runs Internet content in Protected Mode, a sandbox with an isolated cookie store. In order to get a cookie from the Protected Mode sandbox from an external application running at Medium integrity (aka outside of Internet Explorer), you must use the IEGetProtectedModeCookie function. This API was added in IE8, and unfortunately, there is no straightforward alternative for IE7. This API has a number of limits, in particular, it cannot be called by processes running at High Integrity (Admin).
IE10+ on Windows 8+ introduced Enhanced Protected Mode which uses AppContainers (rather than Integrity Levels) for isolation. EPM does not offer an API for interacting with cookies; IEGetProtectedModeCookie will not get the cookie inside an AppContainer.
Q12: How can I log into my web application multiple times? How are cookies shared between IE windows?
A: Good question, and the answer has changed a bit in IE8. I wrote up a full post on the subject over on the IEBlog.
Q13: How can I control (block, downgrade or allow) cookies?
A: See Internet Explorer Cookie Controls.
That’s it for now; thanks for reading!
>Can I reliably assume that the first one is for x.y.z?
> Does $_COOKIE["cookiename"] always contain the first cookie named cookiename?
Sorry, you'd have to ask a PHP expert for the answer to that. I would assume so, but I don't use PHP.
Given a domain zz.no, with sub domains auth.zz.no, community.zz.no...
How can I make IE accept cookies set from auth.zz.no to work for all sub domains (incl parent domain zz.no) ??
".zz.no" works in other browsers, but does not seem to work with IE.
@andrerom: See Q6 above, and the post that I link to in the answer
I've read those, only thing you mention is using www on domain or skip specifying domain which won't really work in this case(?)
Then there is the "xx.yy." suggestion on Stack overflow which I haven't tested yet as it seems that you'll have to actually browse to http://xx.yy./ as well and furthermore your in depth blog on it mentions "1> If the final label is empty, drop it for the purposes of this algorithm" suggesting that it probably won't work.
@andrerom: Let me state the conclusion more plainly: For IE versions prior to 8, there is no real workaround for what you are trying to do. For IE8 and later, your scenario will work only if the DomainList mentioned in the linked blog post is updated to include "xx.yy" as a Domain, overriding its default status as a TLD. Given these factors, the simplest reliable approach would be to structure your site such that the default ccTLD rules are followed.
@EricLaw: Thanks, totally overlooked the IE8 part. Theoretically (overlooking that it won't work in IE6 / IE7): who do I contact in MS to report additional domain to add to the domain list?
I wrote a small application that hosts webbrowser control. What this application does is to use open mail API from Yahoo! to allow the application user to be able to access his/her mail box.
Everything works fine on the surface. But soon I encounter this strange problem:
Let's say user A is using this application to sign in and access his mail box.
User B is using IE browser to access his mail box.
Now User B signs out of his account in the IE browser. Suddenly user A gets kicked out this account from the application and needs re-type in his user ID and password.
I use fiddler in trying to figure out what went wrong. It turns out that Yahoo! server issues Y and T cookies every time when user signs in. When user signs out, Yahoo! Server resets the Y&T cookies.
So when user B signs out of his account in the IE browser, the Y&T cookies in the browser get reset. Somehow the webbrowser control in my application picks up this pair of Y&T cookies from IE browser.
In my application I'm using authentication API from Yahoo! to get a pair of Y&T cookies for user A and call InternetSetCookieEx with third-party flag to set the cookies.
So my question is why the webbrowser control picked up the Y&T cookie from IE browser, not the pair set by the application?
@Bing: Generally speaking, this is expected. WebBrowser Controls and IE generally share persistent cookies (session cookies, by their per-process nature, are not shared). Now, the one caveat is that WebOCs and IE may not share cookies when running on Vista+ when Protected Mode is enabled, because Internet sites run at Low in IE and at Medium in most WebOC applications, and there's a partition between the Low and Medium cookie stores.
I am not aware of any easy mechanism to opt-out of sharing the Persistent cookie store between a WebOC application and IE.
From fiddler, Y&T cookies are session cookies because they don't have "expires" attribute.
If so, why WebOC and IE still share Y&T cookies?
@Bing: When Yahoo deletes the cookie, they send an Expires attribute in the past, don't they?
I'm seeing an issue where users with their system clocks set in the future are unable to log into a site. Does IE use the "Date" response header as a base for comparison with a cookie's "expires" value or just use the system clock?
@John: Very interesting question. I don't see any code that attempts to adjust the cookie's Expiration based on the server's DATE header. I'd imagine that such adjustments could cause some problems due to cached server responses on proxies.
It's been a while since I looked at cookies, and I'm only doing so now because System.Gadget seems to be disabled in Vista Home Basic (I think, but can't confirm). Anyway, it seems like Bill Dortch's 1996 work is still valid. But I am puzzled. Am I right in thinking that there is really only one conceptual cookie available to any web site/page/whatever, and that all the information is held as compound elements in a pushdown stack? Nowhere is document.cookie qualified with any name as such, only the crumbs.
@John: I'm afraid I have absolutely no idea what you're talking about.
Today we found a site that was setting a cookie like so:
document.cookie = "a=b;expires=99999"
In some browsers, this will create a session cookie. In IE, this will typically set a cookie that expires in 1 second or less.
In contrast, if you set a cookie like so:
document.cookie = "a=b;expires=99f999"
The Expires parameter will be ignored and a session cookie will be created.
The lesson? Don't set Expires if you want a Session cookie. If you want a Persistent cookie, send a well-formed Expires parameter.