【问题标题】:Cross Sub Domain Cookie跨子域 Cookie
【发布时间】:2015-02-21 00:32:57
【问题描述】:

我正在尝试跨子域携带我的 cookie,但我似乎无法携带它。我希望 cookie 中的域是 .apus.edu,但 cookie 是 catalog.apus.edu、amu.apus.edu 或 apu.apus.edu

这是我的起始页。您将被分配一个默认 cookie http://catalog.apus.edu/sitecat/cookie.htm

当您点击 AMU 时,您将被分配 AMU Cookie 等。

当您到达控制页面时,您应该拥有 AMU 或 APU,否则我会为您分配默认值。

我真的希望域是 .apus.edu 而不是每个子域。

function Set_Cookie( name, value, expires, path, domain, secure ) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for ( i = 0; i < a_all_cookies.length; i++ ) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split( '=' );

        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if ( cookie_name == check_name ) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if ( a_temp_cookie.length > 1 ) {
                cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if ( !b_cookie_found ) {
        return null;
    }
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
    if ( Get_Cookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

var CookieNameDefault = 'Default Cookie';
var CookieValueDefault = 'Default Cookie';

var CookieNameAMU = 'AMU Cookie';
var CookieValueAMU = 'AMU Cookie';

var CookieNameAPU = 'APU Cookie';
var CookieValueAPU = 'APU Cookie';

var cAMU = Get_Cookie(CookieNameAMU); 
var cAPU = Get_Cookie(CookieNameAPU); 

if (window.location.href.indexOf("www.amu.apus.edu/_test/sitecat/cookie.htm") >= 0) { //delete APU & Default cookie and set AMU cookie
    Delete_Cookie(CookieNameAPU, '/', '');
    Delete_Cookie(CookieNameDefault, '/', '');
    Set_Cookie(CookieNameAMU, CookieValueAMU, '', '/', '', '');
    alert(CookieNameAMU);
}

if (window.location.href.indexOf("www.apu.apus.edu/_test/sitecat/cookie.htm") >= 0) { //delete AMU & Default cookie and set APU cookie
    Delete_Cookie(CookieNameAMU, '/', '');
    Delete_Cookie(CookieNameDefault, '/', '');
    Set_Cookie(CookieNameAPU, CookieValueAPU, '', '/', '', '');
    alert(CookieNameAPU);
}

if (cAMU || cAPU) { 
    // check if you have any of these two cookies if so then do nothing
}
else { // if you don't have any cookie then lets give you the default cookie
    Set_Cookie( CookieNameDefault, CookieValueDefault, '', '/', '', '' ); 
    alert(CookieNameDefault);
}

【问题讨论】:

    标签: javascript cookies


    【解决方案1】:

    好吧,我不能 100% 确定那一大段代码中发生了什么。但是,如果您将 cookie 域设置为 .apus.edu,那么它将可用于 *.apus.edu

    I.E.你在哪里:

    Set_Cookie(CookieNameAMU, CookieValueAMU, '', '/', '', '');
    

    应该是

    Set_Cookie(CookieNameAMU, CookieValueAMU, '', '/', '.apus.edu', '');
    

    【讨论】:

    • 成功了!我很感激。我还想问一下,当我关闭浏览器时,我注意到我的 cookie 会自行重置回我的默认 cookie。我认为 cookie 的重点是在您删除 cookie(或它们过期)之前,当您返回站点时 cookie 会一直存在?
    • @Evan cookie 应该是持久的。使用 chrome 调试工具中的资源管理器检查 cookie。
    • 现在它是持久的,我指定它不会过期,直到一整年。由于没有日期,它在浏览器关闭时到期。 Set_Cookie(CookieNameAMU, CookieValueAMU, '365', '/', '.apus.edu', '');再次感谢您的大力帮助。我现在有一个完整的跟踪系统。
    【解决方案2】:

    您应该指明可以读取此 cookie 的更高域 如果你设置 catalog.apus.edu ,它可以被 subdomain.catalog.apus.edu 访问,但不能被 apus.edu 和 other.apus.edu 访问

    所以,必须将域设置为 apus.edu ,这样才能被 apus.edu 、 www.apus.edu 、 anysubdomain.apus.edu 访问,...

    Set_Cookie(CookieNameAMU, CookieValueAMU, '', '/', 'apus.edu' , '');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-19
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      相关资源
      最近更新 更多