title: Creating a cookie using PHP but having difficulty reading from javascript
body: <p>I have set my cookie in PHP using the following:</p>
  
  <pre><code>setcookie("id", 100, time()+100000, "/AP", "www.mydomain.com", 0, true);
  </code></pre>
  
  <p>When I look at the cookies stored in the browser it looks like this:</p>
  
  <pre><code>Name:    id
  Content: 100
  Domain:  .www.mydomain.com
  Path:    /AP
  </code></pre>
  
  <p><strong>Notice the . in the Domain</strong></p>
  
  <p>When I set a cookie in javascript I get the same results except:</p>
  
  <pre><code>Name:    id
  Content: 100
  Domain:  www.mydomain.com
  Path:    /AP
  </code></pre>
  
  <p>The domain is different.  Why does my PHP cookie put a '.' in front of  www.mydomain.com and javascript does not.  </p>
  
  <p>The following is the javascript code that I'm using to create a cookie:</p>
  
  <pre><code>            function SetCookie(cookieName,cookieValue,nDays) {
               var today = new Date();
               var expire = new Date();
               if (nDays==null || nDays==0) nDays=1;
               expire.setTime(today.getTime() + 3600000*24*nDays);
               document.cookie = cookieName+"="+escape(cookieValue)
                               + ";expires="+expire.toGMTString();
          }
  </code></pre>
  
  <p>Any ideas? </p>
  
  <p><strong>Update:</strong></p>
  
  <p>When I try to read this using the following function in javascript:</p>
  
  <pre><code>            function ReadCookie(cookieName) {
               var theCookie=""+document.cookie;
               var ind=theCookie.indexOf(cookieName);
               if (ind==-1 || cookieName=="") return "";
               var ind1=theCookie.indexOf(';',ind);
               if (ind1==-1) ind1=theCookie.length;
               return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
          }
  </code></pre>
  
  <p>I can't get the value using the ReadCookie function (above) from the cookie that contains:</p>
  
  <pre><code>Domain: .www.mydomain.com
  </code></pre>
  
  <p>However the cookie that contains:</p>
  
  <pre><code>Domain: www.mydomain.com 
  </code></pre>
  
  <p>works just fine.</p>