Writing cookies from include file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael D. Kersey

    #16
    Re: Writing cookies from include file

    Brian Burgess wrote:[color=blue]
    > Hi Michael,
    > So what if then page1.asp Server.Executes page2.asp in a loop. And
    > the cookies are then set in page2.asp .. should the cookies not then
    > write?[/color]

    The cookies aren't available until
    - they are sent to the browser as part of a Response AND
    - the browser sends a new Request.

    Here's an example of how cookies work:
    0. Browser and server establish a connection,
    1. Browser sends request to server,
    2. Server ASP code stores cookie in Response object,
    3. Server sends contents of response object to browser,
    4. Browser accepts response contents and stores cookie,
    5. Browser and server close connection.
    Only now is the cookie is available for any *subsequent* HTTP requests.

    What you are trying to do (but which won't work):
    0. Browser and server establish a connection,
    1. Browser sends request to server,
    2. Server ASP code stores cookie in *Response* object,
    3. Server ASP code tries to read same cookie from *Request* object, but
    it isn't there!
    ....
    Writing a cookie to the Response object does not mean that it is
    available immediately (in the same script) from the *Request* object.

    Also note that Server.Execute does not establish a new connection; it
    uses the same connection as the calling script.

    If you want to pass variables from one script to the other, then use
    Session or Application variables.

    Good Luck,
    Michael D. Kersey


    [color=blue]
    > On Fri, 12 Sep 2003 03:52:45 -0500, "Michael D. Kersey"
    > <mdkersey@hal-pc.org> wrote:
    >[color=green]
    > >Brian Burgess wrote:[color=darkred]
    > >> So I can do:
    > >> <%
    > >> Response.Cookie s("cookie1") = "blah"
    > >> Response.Cookie s("cookie1").Ex pires=DateAdd(" n", 15, Now)[/color]
    > >
    > >This code builds a cookie in the Response object, but it does not send
    > >the cookie to the browser yet. The cookie (which is an HTTP "Set-Cookie"
    > >header) will be sent to the browser after the script terminates.
    > >[color=darkred]
    > >> Server.Execute( "page2.asp" )
    > >> ..
    > >> %>
    > >>
    > >> Then in page2.asp I can do something like:
    > >> <%
    > >> Dim strCookie1
    > >> strCookie1 = Request.Cookies ("cookie1")
    > >> %>[/color]
    > >
    > >Since the cookie has not yet been sent to the browser, the Request
    > >object will not be able to fetch that cookie. Only after your script
    > >terminates will the cookie be sent.
    > >
    > >Good Luck,
    > >Michael D. Kersey[/color][/color]

    Comment

    • Jacob Yang [MSFT]

      #17
      Re: Writing cookies from include file

      Hi Brian,

      I agree with your current troubleshooting method (starting from a simple
      page and building it up to be more and more like your production page). I
      suggest that you continue building this new page.

      As I have been unable to reproduce your problem, I don't have anything more
      to add. Please let me know if you have more information which will allow me
      to assist you further.

      Best regards,

      Jacob Yang
      Microsoft Online Partner Support
      <MCSD>
      Get Secure! ¨C www.microsoft.com/security
      This posting is provided "as is" with no warranties and confers no rights.

      Comment

      • Brian Burgess

        #18
        Re: Writing cookies from include file

        Well then my understanding of cookies is correct. What I am trying to
        do is save some info in case a disconnect occurs at some point during
        the loop. This is the reason for cookies, instead of session
        variables. But you also mention something about Server.Execute:
        This does send a request right? ..requesting a page? .. not
        necessarily a new connection. In my test asp I can write without
        closing the browser. Do you think explicitly calling Response.End at
        the end of the Server.Executed page will work?

        thanks..

        -BB

        On Sun, 14 Sep 2003 11:39:54 -0500, "Michael D. Kersey"
        <mdkersey@hal-pc.org> wrote:
        [color=blue]
        >Brian Burgess wrote:[color=green]
        >> Hi Michael,
        >> So what if then page1.asp Server.Executes page2.asp in a loop. And
        >> the cookies are then set in page2.asp .. should the cookies not then
        >> write?[/color]
        >
        >The cookies aren't available until
        >- they are sent to the browser as part of a Response AND
        >- the browser sends a new Request.
        >
        >Here's an example of how cookies work:
        >0. Browser and server establish a connection,
        >1. Browser sends request to server,
        >2. Server ASP code stores cookie in Response object,
        >3. Server sends contents of response object to browser,
        >4. Browser accepts response contents and stores cookie,
        >5. Browser and server close connection.
        >Only now is the cookie is available for any *subsequent* HTTP requests.
        >
        >What you are trying to do (but which won't work):
        >0. Browser and server establish a connection,
        >1. Browser sends request to server,
        >2. Server ASP code stores cookie in *Response* object,
        >3. Server ASP code tries to read same cookie from *Request* object, but
        >it isn't there!
        >...
        >Writing a cookie to the Response object does not mean that it is
        >available immediately (in the same script) from the *Request* object.
        >
        >Also note that Server.Execute does not establish a new connection; it
        >uses the same connection as the calling script.
        >
        >If you want to pass variables from one script to the other, then use
        >Session or Application variables.
        >
        >Good Luck,
        >Michael D. Kersey
        >
        >
        >[color=green]
        >> On Fri, 12 Sep 2003 03:52:45 -0500, "Michael D. Kersey"
        >> <mdkersey@hal-pc.org> wrote:
        >>[color=darkred]
        >> >Brian Burgess wrote:
        >> >> So I can do:
        >> >> <%
        >> >> Response.Cookie s("cookie1") = "blah"
        >> >> Response.Cookie s("cookie1").Ex pires=DateAdd(" n", 15, Now)
        >> >
        >> >This code builds a cookie in the Response object, but it does not send
        >> >the cookie to the browser yet. The cookie (which is an HTTP "Set-Cookie"
        >> >header) will be sent to the browser after the script terminates.
        >> >
        >> >> Server.Execute( "page2.asp" )
        >> >> ..
        >> >> %>
        >> >>
        >> >> Then in page2.asp I can do something like:
        >> >> <%
        >> >> Dim strCookie1
        >> >> strCookie1 = Request.Cookies ("cookie1")
        >> >> %>
        >> >
        >> >Since the cookie has not yet been sent to the browser, the Request
        >> >object will not be able to fetch that cookie. Only after your script
        >> >terminates will the cookie be sent.
        >> >
        >> >Good Luck,
        >> >Michael D. Kersey[/color][/color][/color]

        Comment

        • Brian Burgess

          #19
          Re: Writing cookies from include file

          Hi Jacob,

          Yes I'm not able (so far) to reproduce it anywhere else either.
          Funny how these things always only occur in production eh? ;-)


          -BB

          On Tue, 16 Sep 2003 01:17:02 GMT, jiany@online.mi crosoft.com (Jacob
          Yang [MSFT]) wrote:
          [color=blue]
          >Hi Brian,
          >
          >I agree with your current troubleshooting method (starting from a simple
          >page and building it up to be more and more like your production page). I
          >suggest that you continue building this new page.
          >
          >As I have been unable to reproduce your problem, I don't have anything more
          >to add. Please let me know if you have more information which will allow me
          >to assist you further.
          >
          >Best regards,
          >
          >Jacob Yang
          >Microsoft Online Partner Support
          ><MCSD>
          >Get Secure! ¨C www.microsoft.com/security
          >This posting is provided "as is" with no warranties and confers no rights.[/color]

          Comment

          • Michael D. Kersey

            #20
            Re: Writing cookies from include file

            Brian Burgess wrote:[color=blue]
            > What I am trying to
            > do is save some info in case a disconnect occurs at some point during
            > the loop.[/color]

            Try setting Response.Buffer = FALSE at the top of your page and/or doing
            a Response.Flush after each cookie is set.

            A cookie is an HTTP header type. HTTP headers are sent as part of an
            HTTP request/response, but since they are headers, they must be sent
            *before* any other page output is sent.

            So if the ASP script is outputting cookies, it cannot output any other
            page data (no starting <HTML> tag, no Response.Write' s, not even a
            single space) until *after* all cookies have been output. With buffering
            disabled, the server should send the cookies to the browser as soon as
            they are created. I am however unclear as to what is the obligation of
            the browser (i.e., whether it should accept or reject cookies associated
            with a connection) if a connection is abnormally terminated.

            By default, buffering is enabled (Response.Buffe r = TRUE) and cookies
            would not be sent until either:
            - the ASP page completes,
            - a Response.Flush is issued or
            - a Response.End is issued.
            [color=blue]
            > This is the reason for cookies, instead of session
            > variables.[/color]

            IMO it would be safer and faster to save the loop counter in a session
            variable. Since you are using cookies, Session variables are enabled
            anyway, so why not use them?
            [color=blue]
            > But you also mention something about Server.Execute:
            > This does send a request right? ..requesting a page? .. not
            > necessarily a new connection.[/color]

            No, it does not send a request. Server.Execute( xxx) is a server-side
            function that executes the contents of page xxx inline (it acts like a
            "call" to that page's code). IOW it is as if xxx was part of the calling
            page.

            Good Luck,
            Michael D. Kersey

            Comment

            • Brian Burgess

              #21
              Re: Writing cookies from include file

              Ok well that's the trouble then .. I might have to do a Transfer or
              Redirect then..

              Thanks!

              -BB

              On Tue, 16 Sep 2003 12:49:37 -0500, "Michael D. Kersey"
              <mdkersey@hal-pc.org> wrote:
              [color=blue]
              >No, it does not send a request. Server.Execute( xxx) is a server-side
              >function that executes the contents of page xxx inline (it acts like a
              >"call" to that page's code). IOW it is as if xxx was part of the calling
              >page.[/color]

              Comment

              Working...