跨子域共享cookie无效

时间:2018-02-06 06:33:28

标签: javascript php

我将访问者的样式表首选项存储在Cookie中,但它运行正常,但似乎并不希望将Cookie共享到子域。

我尝试过指定域但是没有用。我做错了什么?

  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;
  }

1 个答案:

答案 0 :(得分:0)

我设法弄清楚了,下面是更新的(工作)代码:

  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;
  }