title: How do browser cookie domains work?
body: <p>Due to weird domain/subdomain cookie issues that I'm getting, I'd like to know how browsers handle cookies. If they do it in different ways, it would also be nice to know the differences.</p>
  
  <p>In other words - when a browser receives a cookie, that cookie MAY have a domain and a path attached to it. Or not, in which case the browser probably substitutes some defaults for them. Question 1: what are they?</p>
  
  <p>Later, when the browser is about to make a request, it checks its cookies and filters out the ones it should send for that request. It does so by matching them against the requests path and domain. Question 2: what are the matching rules?
  <hr/>
  <strong>Added:</strong></p>
  
  <p>The reason I'm asking this is because I'm interested in some edge cases. Like:</p>
  
  <ul>
  <li>Will a cookie for <code>.example.com</code> be available for <code>www.example.com</code>?</li>
  <li>Will a cookie for <code>.example.com</code> be available for <code>example.com</code>?</li>
  <li>Will a cookie for <code>example.com</code> be available for <code>www.example.com</code>?</li>
  <li>Will a cookie for <code>example.com</code> be available for <code>anotherexample.com</code>?</li>
  <li>Will <code>www.example.com</code> be able to set cookie for <code>example.com</code>?</li>
  <li>Will <code>www.example.com</code> be able to set cookie for <code>www2.example.com</code>?</li>
  <li>Will <code>www.example.com</code> be able to set cookie for <code>.com</code>?</li>
  <li>Etc.</li>
  </ul>
  
  <p><strong>Added 2:</strong></p>
  
  <p>Also, could someone suggest how I should set a cookie so that:</p>
  
  <ul>
  <li>It can be set by either <code>www.example.com</code> or <code>example.com</code>;</li>
  <li>It is accessible by both <code>www.example.com</code> and <code>example.com</code>.</li>
  </ul>