简体   繁体   中英

Sharing cookie across subdomains not working

I have my visitors' style sheet preference stored in a cookie, It's working fine, however, it doesn't seem to want to share the cookie across to the subdomains.

I tried specifying the domain but that didn't work. What am I doing wrong?

  function setcookie( name, value, expiry, path, domain ) {
      if(expiry) {
          var now = new Date();
          now.setTime( now.getTime() + Math.round(86400000*expiry) );
          expiry = now.toGMTString();
      }
      domain = '.mydomain.com';
      expiry = expiry ? '; expires=' + expiry : '';
      path = path ?'; path=' + path:'';
      document.cookie = name + '=' + escape(value) + expiry + path;
  }

I managed to figure it out, below is the updated (working) code:

  function setcookie( name, value, expiry, path, domain ) {

      if (domain)
    cookie += "domain=.mydomain.com" + domain + ";";

      if(expiry) {
          var now = new Date();
          now.setTime( now.getTime() + Math.round(86400000*expiry) );
          expiry = now.toGMTString();
      }

      expiry = expiry ? '; expires=' + expiry : '';
      path = path ?'; path=' + path:'';
      document.cookie = name + '=' + escape(value) + expiry + path + domain;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM