setting cookies across subdomains or paths

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jose Olivas

    setting cookies across subdomains or paths

    I am setting a cookie on a subdomain:


    Then the store takes me to a shopping cart that is at:


    Somewhere the following code (taken and modified from The JavaScript Source)
    is broken between domains, because when I click my "store" link that should
    read the cookie and send me to store1.mydomain .com or storeN.mydomain .com I
    get the default template store.

    <SCRIPT LANGUAGE="JavaS cript">
    <!-- Original: Ronnie T. Moore -->
    <!-- Web Site: The JavaScript Source -->

    <!-- Begin
    var expDays = 30;
    var exp = new Date();
    exp.setTime(exp .getTime() + (expDays*24*60* 60*1000));

    function getCookieVal (offset) {
    var endstr = document.cookie .indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie .length;
    return unescape(docume nt.cookie.subst ring(offset, endstr));
    }
    function GetCookie (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie .length;
    var i = 0;
    while (i < clen) {
    var j = i + alen;
    if (document.cooki e.substring(i, j) == arg)
    return getCookieVal (j);
    i = document.cookie .indexOf(" ", i) + 1;
    if (i == 0) break;
    }
    return null;
    }
    function SetCookie (name, value) {
    var argv = SetCookie.argum ents;
    var argc = SetCookie.argum ents.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTSt ring())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
    }
    function DeleteCookie (name) {
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    var cval = GetCookie (name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString ();
    }

    var favorite = GetCookie('stor e');

    if (favorite != null) {
    switch (favorite) {
    case 'store1' : url = 'http://store1.mydomain .com/';
    break;
    case 'store2' : url = 'http://store2.mydomain .com/';
    break;
    case 'store3' : url ='http://store3.mydomain .com/';
    break;
    case 'storeN' : url = 'http://storeN.mydomain .com/';
    break;
    }
    window.location .href = url;
    }
    // End -->
    </script>
    </HEAD>

    The cookie is set by a javascript "onload command" Like I said I have it
    working on a server with no sub domain.

    Any help would be truly appreciated.

    Jose Olivas


  • Alvaro G Vicario

    #2
    Re: setting cookies across subdomains or paths

    *** Jose Olivas wrote/escribió (Mon, 14 Mar 2005 23:30:18 -0700):[color=blue]
    > I am setting a cookie on a subdomain:
    > http://store1.mydomain.com
    >
    > Then the store takes me to a shopping cart that is at:
    > http://
    >
    > Somewhere the following code (taken and modified from The JavaScript Source)
    > is broken between domains[/color]

    I couldn't find the line where you set the cookie value. Anyway, there're a
    couple of things about cookies that must be taken into account:

    1) Cookies are per-domain settings: they're only sent by browser to pages
    in the domain they're attached to. If you need a cookie to be valid in more
    than one domain then you need to set more than one cookie, one per domain.

    2) Many browsers are configured to discard cookies for third-party domains.
    So if you set a cookie at store1.mydomain .com for shopping.mydoma in.com
    they cookie will most likely be lost.

    If you have access to a server-side language you may consider using
    sessions or databases.


    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- Don't e-mail me your questions, post them to the group
    --

    Comment

    Working...