Cookie writing confusion

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

    #1

    Cookie writing confusion

    Hello,

    Please help.

    I keep data in several session variables, one of which is a complex
    object tied to a custom DLL I wrote. Everything seems to be running
    fine until I introduced a system to remember certain data to cookies.

    At some point in my application the following code is executed

    WriteCookie(key , value, new Date(((new Date()).getTime () +
    (365*86400000)) ), "/")

    Almost immediately after, the session variable holding the complex
    object no longer exists (vartype of object = 0). I commented out that
    line in the app and the session variable continues to exist.

    I am not sure how the session variable and my writing of the cookie is
    inter-related. Can someone please explain to me what might be
    happening, why and how I can deal with it ?

    Thank you,
    Charly
  • kaeli

    #2
    Re: Cookie writing confusion

    In article <30e61e4d.04041 62151.2fe158a@p osting.google.c om>,
    walterware@aol. com enlightened us with...[color=blue][color=green]
    >>[/color]
    > At some point in my application the following code is executed
    >
    > WriteCookie(key , value, new Date(((new Date()).getTime () +
    > (365*86400000)) ), "/")
    >[/color]

    Sessions often store a unique ID on the client computer as a cookie
    (some use URL rewriting), which the server uses to look up data with
    each request. I suspect a problem with the interaction between the
    session cookie and the one you are writing.
    What exactly does WriteCookie look like? Got code?


    --
    --
    ~kaeli~
    A man's home is his castle..., in a manor of speaking.



    Comment

    • Charly Walter

      #3
      Re: Cookie writing confusion

      Thank you for your reply.

      I presumed as much since I do see a stub of something when I open the
      cookie in a text editor.

      The cookie writing code is as follows and is nothing out of the
      ordinary, I think:

      function WriteCookie(sNa me, sValue, dExpires, sPath, sDomain, bSecure) {
      var sCookie;
      var sError;

      sCookie = sError = "";


      if(sError.lengt h == 0){
      sCookie += escape(sName) + "=";
      sCookie += escape(sValue);
      if(dExpires)sCo okie += "; expires=" + dExpires.toGMTS tring();
      if(sPath) sCookie += "; path=" + sPath;
      if(sDomain) sCookie += "; domain=" + sDomain;
      if(bSecure) sCookie += "; secure=" + bSecure.toStrin g();

      document.cookie = sCookie;

      if(sValue != GetCookie(sName )) sError = sError + "Write failure/n";
      }
      return sError;
      }

      Regards,
      Charly




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

      Comment

      • kaeli

        #4
        Re: Cookie writing confusion

        In article <4083fbbb$0$206 $75868355@news. frii.net>, walterware@aol. com
        enlightened us with...[color=blue]
        > Thank you for your reply.
        >
        > I presumed as much since I do see a stub of something when I open the
        > cookie in a text editor.[/color]

        What name are you calling it?
        Have you tried calling it something else?
        [color=blue]
        >
        > The cookie writing code is as follows and is nothing out of the
        > ordinary, I think:
        >
        > function WriteCookie(sNa me, sValue, dExpires, sPath, sDomain, bSecure) {
        > var sCookie;
        > var sError;
        >
        > sCookie = sError = "";
        >
        >
        > if(sError.lengt h == 0){[/color]

        This will always be true, since you just set it to the empty string,
        above. What's the point?
        [color=blue]
        > sCookie += escape(sName) + "=";
        > sCookie += escape(sValue);
        > if(dExpires)sCo okie += "; expires=" + dExpires.toGMTS tring();[/color]

        Is this a copy/paste error? There may need to be a space after that
        close parens.
        [color=blue]
        > if(sPath) sCookie += "; path=" + sPath;
        > if(sDomain) sCookie += "; domain=" + sDomain;
        > if(bSecure) sCookie += "; secure=" + bSecure.toStrin g();
        >
        > document.cookie = sCookie;[/color]

        Whoops. You just wiped out the previous value of document.cookie . Bad
        kitty.

        document.cookie += sCookie.
        That was probably your problem.

        --
        --
        ~kaeli~
        Jesus saves, Allah protects, and Cthulhu thinks you'd make
        a nice sandwich.



        Comment

        • Michael Winter

          #5
          Re: Cookie writing confusion

          On Mon, 19 Apr 2004 12:31:29 -0500, kaeli <tiny_one@NOSPA M.comcast.net>
          wrote:
          [color=blue]
          > In article <4083fbbb$0$206 $75868355@news. frii.net>, walterware@aol. com
          > enlightened us with...[/color]

          [snip]
          [color=blue][color=green]
          >> if(dExpires)sCo okie += "; expires=" + dExpires.toGMTS tring();[/color]
          >
          > Is this a copy/paste error? There may need to be a space after that
          > close parens.[/color]

          That shouldn't matter. It would be more readable, though.
          [color=blue][color=green]
          >> if(bSecure) sCookie += "; secure=" + bSecure.toStrin g();[/color][/color]

          This is actually incorrect. The "secure" field is a boolean in itself. If
          it is present, the cookie should be made secure. If it is absent, the
          cookie can be stored unaltered. Change it to

          if( bSecure ) sCookie += '; secure';

          From the OP:

          WriteCookie(
          key,
          value,
          new Date(((new Date()).getTime () + (365*86400000)) ),
          "/" );

          It's probably better to take advantage of Date()'s ability to roll over
          automatically:

          var nextYear = new Date();
          nextYear.setFul lYear( nextYear.getFul lYear() + 1 );

          WriteCookie(
          key,
          value,
          nextYear,
          "/" );

          --
          Michael Winter
          M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

          Comment

          • Charly Walter

            #6
            Re: Cookie writing confusion

            Thank you Kaeli and Michael !!

            I was thinking of cookie arcania and my problem seemed to be application
            of syntax.

            Of course, your suggestions did the trick. And I did learn enough to
            actually remember some.

            Thanks again,
            Charly



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

            Comment

            Working...