Finding cookie name?

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

    Finding cookie name?



    When I reference document.cookie , there is a long string of key=value;
    pairs listed. I may have 100 hundred cookies on my hard drive.
    However, most only have one key=value pair. Does the document.cookie
    variable combine all cookie key=value pairs?

    All of the examples I've seen discuss referencing a specific cookie. I
    don't see how this is done.

    Cookies are usually named by the domain. If I want to reference a
    specific cookie, do I need to search for my key=value pair by parsing
    the document.cookie string? If I have several key=value; pairs in one
    cookie, must I continue parsing for those specific key=value; pairs?

    Judging by most getCookie() functions I've seen, the above seems to be
    the case. I can't just call a specific cookie. I must reference
    document.cookie and search for my specific key=value pairs. However, I
    may be going about it the wrong way. Please clarify.

    Thanks,
    Brett

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • kaeli

    #2
    Re: Finding cookie name?

    In article <410d797f$0$559 3$c397aba@news. newsgroups.ws>, myaccount@cygen .com
    enlightened us with...[color=blue]
    > Does the document.cookie
    > variable combine all cookie key=value pairs?
    >[/color]

    Yes, it does.
    [color=blue]
    > All of the examples I've seen discuss referencing a specific cookie. I
    > don't see how this is done.
    >[/color]

    There are a couple ways to do it.
    [color=blue]
    > Cookies are usually named by the domain.[/color]

    Not always.
    [color=blue]
    > If I want to reference a
    > specific cookie, do I need to search for my key=value pair by parsing
    > the document.cookie string? If I have several key=value; pairs in one
    > cookie, must I continue parsing for those specific key=value; pairs?
    >[/color]

    Yes.

    Here's my cookie.js file if it helps you. You can get your cookie into a
    string by using its name this way:
    var myCk = getCookie("myCo okieName");

    Watch for word-wrapping.
    Note that I don't bother with setting a lot of the attributes, such as
    domain, as I don't need to. You could modify this to do that.

    --------------------------------------------------------------

    /* jsCookies.js
    This file contains cookie functions. */
    /* File Functions:
    1. setCookie - writes cookie
    2. getCookie - gets value of cookie
    3. removeCookie - deletes a cookie
    4. detectCookies - checks if cookies are enabled
    */

    function setCookie(cooki eName, cookieValue, expireDate)
    {
    /* Pass in three strings - the name of the cookie, the value, and the
    expire date.
    Pass in a "" empty string for expireDate to set a session cookie (no
    expires date).
    Pass in any other date for expire as a number of days to be added to
    today's date. */

    if (expireDate == "")
    {
    expires = "";
    }
    else
    {
    expires = new Date();
    expires.setDate (expires.getDat e() + expireDate);
    expires = expires.toGMTSt ring();
    }
    document.cookie = cookieName+"="+ cookieValue+";e xpires="+expire s;
    }

    function removeCookie (cookieName)
    {
    /* Pass in the name of the cookie as a string and it will be removed. */
    expires = Now();
    document.cookie = cookieName+"= ;expires="+expi res.toGMTString ();
    }

    function getCookie (cookieName)
    {
    cookieValue = ""
    if (document.cooki e.indexOf(cooki eName) == -1)
    {
    // there is no cookie by this name for this user
    return cookieValue;
    }
    else
    {
    // get the beginning index of the cookie by looking for the cookie name
    cookieStart = document.cookie .indexOf(cookie Name);
    // get the beginning index of the cookie value by looking for the equal
    sign after the name
    cookieValStart = (document.cooki e.indexOf("=", cookieStart) + 1);
    // get the end index of the cookie value by looking for the semi-colon
    after the value
    cookieValEnd = document.cookie .indexOf(";", cookieStart);
    // if no semi-colon, then use the whole length
    if (cookieValEnd == -1)
    {
    cookieValEnd = document.cookie .length
    }
    // use substring to get the text between the two indices and that is
    the value of the cookie
    cookieValue = document.cookie .substring(cook ieValStart, cookieValEnd);
    return cookieValue;
    }
    }

    function detectCookies()
    {
    /* function returns true if cookies are enables, false if not */
    setCookie("test ", "test", "");
    tmp = getCookie("test ")
    if (tmp != "test")
    {
    return false;
    }
    else
    {
    return true;
    }
    }

    /* EOF */
    -----------------------------------------------------------------------

    --
    --
    ~kaeli~
    Never mess up an apology with an excuse.



    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Finding cookie name?

      brettr wrote:[color=blue]
      > When I reference document.cookie , there is a long string of key=value;
      > pairs listed. I may have 100 hundred cookies on my hard drive.[/color]

      You maybe want to remove some because it is likely that there are spies
      among them.
      [color=blue]
      > However, most only have one key=value pair. Does the document.cookie
      > variable combine all cookie key=value pairs?[/color]

      All for one resource, when reading the property, yes.
      [color=blue]
      > All of the examples I've seen discuss referencing a specific cookie.[/color]

      URL?
      [color=blue]
      > I don't see how this is done.[/color]

      You could split the string value and then search for the respective key.
      It then helps to split the key/value pairs again on the "=" and then
      search for the key. Examples have been posted here before.
      [color=blue]
      > Cookies are usually named by the domain. If I want to reference a
      > specific cookie, do I need to search for my key=value pair by parsing
      > the document.cookie string? If I have several key=value; pairs in one
      > cookie, must I continue parsing for those specific key=value; pairs?[/color]

      Yes, but parsing the whole string may not be the most efficient/viable method.
      [color=blue]
      > Judging by most getCookie() functions I've seen,[/color]

      URL(s)?
      [color=blue]
      > the above seems to be the case. I can't just call a specific cookie.
      > I must reference document.cookie and search for my specific key=value
      > pairs.[/color]

      Yes, indeed.


      PointedEars

      Comment

      • Richard Cornford

        #4
        Re: Finding cookie name?

        kaeli wrote:
        <snip>[color=blue]
        > function getCookie (cookieName)
        > {
        > cookieValue = ""[/color]

        There are quite a lot of global variables in this funciton that really
        should be local.
        [color=blue]
        > if (document.cooki e.indexOf(cooki eName) == -1)[/color]
        <snip>

        The FAQ notes page:-

        <URL: http://jibbering.com/faq/faq_notes/cookies.html >

        - describes an extra safety measure necessitated by the use of content
        inserting/re-writing proxies (firewalls, internet security programs and
        such like) to prevent scripts from using cookies. They do this by
        re-writing - document.cookie - to - document.someth ingElse - in the JS
        source code. As a result the modified property accessor does not resolve
        to a string (it will probably return an undefined value), and an attempt
        to call - indexOf - will error as an undefined value will not
        type-convert into an object.

        Generally code that fails by erroring is undesirable (and potentially
        embarrassing for its author if the user is seeing error messages
        (proxies usually insert an - onerror - handler to suppress error
        messages resulting from their actions)).

        The page above proposes using a - typeof - test to verify that the
        object returned from the property accessor is a string prior to calling
        String object methods on it. A number of alternative safety measures
        might be applied (or combined) to achieve a similar effect.

        It should be possible to negate the effect of content inserting proxies
        by using a bracket notation property accessor with a string value that
        did not resemble "cookie":-

        document["\x63o\x6Fk\x69 e"]

        - (or some other escape sequence based munging of "cookie" ) in the hope
        that the proxy would fail to recognise and replace that. However, if the
        user is running a proxy that is attempting to prevent scripts form
        reading and writing cookies then maybe subverting its actions is not a
        desirable thing to be doing.

        An alternative to using - typeof - to verify that - document.cookie - is
        returning a string value might be to assign - new
        String(document .cookie) - to a local variable. Subsequent String object
        method calls would not then be error-producing, though they would still
        not "work" if the property accessor resolved as an undefined value.
        However, as a safety measure, explicitly converting the primitive value
        returned by the property accessor into a String object would have the
        advantageous side-effect of producing more efficient code. The use of a
        String object method on a string primitive forces an internal
        type-conversions into a String object for each method invocation, so
        explicitly doing that up-front in code that uses many String object
        methods would avoid the need to have it done implicitly on each method
        call:-

        function getCookie (cookieName){
        var cookieValStart,
        cookieValEnd,
        cookieValue = "",
        ck = new String(document .cookie);
        //or - ck = new String(document["\x63o\x6Fk\x69 e"]);
        if(
        ((cookieStart = ck.indexOf(cook ieName)) != -1)&&
        ((cookieValStar t = (ck.indexOf("=" , cookieStart) + 1)) != 0)
        ){
        // get the end index of the cookie value by looking
        //for the semi-colon after the value
        cookieValEnd = ck.indexOf(";", cookieStart);
        // if no semi-colon, then use the whole length
        if (cookieValEnd == -1){
        cookieValEnd = ck.length;
        }
        // use substring to get the text between the two
        //indices and that is the value of the cookie
        cookieValue = ck.substring(co okieValStart, cookieValEnd);
        }
        return cookieValue;
        }

        Richard.


        Comment

        • brettr

          #5
          Re: Finding cookie name?

          Maybe the wording is throwing me off. I see the above code referencing
          a cookiename variable being passed into somthing similar to getCookie
          (cookieName). If you can't actually reference a specific cookie, why
          use something called cookieName?

          From what I gather, you can only reference document.cookie and parse it
          for your key/value pairs.

          Must the expires= be set for each key/value pair? Does this create the
          cookie? document.cookie is a variable that grabs key/value pairs and
          other information from .txt files and groups them all into one variable.
          Ok, from that, how are you defining cookie?

          Thanks,
          Brett

          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Michael Winter

            #6
            Re: Finding cookie name?

            On 02 Aug 2004 21:25:09 GMT, brettr <myaccount@cyge n.com> wrote:
            [color=blue]
            > Maybe the wording is throwing me off. I see the above code referencing
            > a cookiename variable being passed into somthing similar to getCookie
            > (cookieName). If you can't actually reference a specific cookie, why
            > use something called cookieName?[/color]

            You pass the function a string containing the name of the key/value pair
            you want to retrieve.
            [color=blue]
            > From what I gather, you can only reference document.cookie and parse it
            > for your key/value pairs.[/color]

            Which is what the function does. The variable, cookieName, is the key for
            the pair.
            [color=blue]
            > Must the expires= be set for each key/value pair?[/color]

            No, it's applies to all the pairs you specify in that assignment. If you
            do not specify an expiry value, the cookie becomes a session cookie. If
            you do specify a date which is in the past, the included pairs are deleted.
            [color=blue]
            > Does this create the cookie?[/color]

            Just writing the document.cookie property creates the cookie (unless the
            expiry date is in the past).
            [color=blue]
            > document.cookie is a variable that grabs key/value pairs and
            > other information from .txt files and groups them all into one variable.
            > Ok, from that, how are you defining cookie?[/color]

            I think you'll find it varies. Some define a cookie as a collection of
            key/value pairs that exist for a specific path on a specific domain. Some
            will define each individual pair as a cookie. I believe the former is the
            more accurate definition, but it doesn't really matter.

            Hope that helps,
            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail

            Comment

            Working...