Disconnecting client browser from php file

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

    Disconnecting client browser from php file

    I'd like to initiate a php script using a standard httpd request, but
    then allow the client browser to disconnect without terminating the
    script. Freeing up the browser to access other functions on the site.

    Whilst a combination of connection_time out() and ignore_user_abo rt()
    can stop the script from terminating when the client calls
    termination, i'd like to terminate the clients connection rather than
    having to wait for the client to do it.

    i.e.

    <?
    echo "Cleanup initiated";
    return_control_ to_client_brows er_as_if_script _terminated();
    run_verylong_du ll_cleanup_util ity_client_need _not_know_about ():
    ?>
  • Tony Marston

    #2
    Re: Disconnecting client browser from php file


    "chris" <chris@remotego at.co.uk> wrote in message
    news:104f1614.0 405070401.606e3 6a2@posting.goo gle.com...[color=blue]
    > I'd like to initiate a php script using a standard httpd request, but
    > then allow the client browser to disconnect without terminating the
    > script. Freeing up the browser to access other functions on the site.[/color]

    I think that your understanding of how server-side scripting languages such
    as PHP work is seriously flawed. When a PHP script generates a web page
    (which is an HTML document) it immediately terminates, so by the time the
    client browser receives the page and starts to render it the PHP script has
    already died. If the user clicks on a hyperlink within the page then this is
    transmitted as a brand new request, not as a continuation of the current
    request. It may be to the same script as before, or it may be a different
    script, but it is a new request.

    There are no persistent connections between a web browser and a web server -
    the HTTP protocol is totally stateless. I do not know what it is you are
    trying to do, and your description leaves to to believe that you don't know
    either.

    --
    Tony Marston
    This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL


    [color=blue]
    > Whilst a combination of connection_time out() and ignore_user_abo rt()
    > can stop the script from terminating when the client calls
    > termination, i'd like to terminate the clients connection rather than
    > having to wait for the client to do it.
    >
    > i.e.
    >
    > <?
    > echo "Cleanup initiated";
    > return_control_ to_client_brows er_as_if_script _terminated();
    > run_verylong_du ll_cleanup_util ity_client_need _not_know_about ():
    > ?>[/color]


    Comment

    • David Mackenzie

      #3
      Re: Disconnecting client browser from php file

      On 7 May 2004 05:01:33 -0700, chris@remotegoa t.co.uk (chris) wrote:
      [color=blue]
      >I'd like to initiate a php script using a standard httpd request, but
      >then allow the client browser to disconnect without terminating the
      >script. Freeing up the browser to access other functions on the site.[/color]

      Browsers will often handle multiple http requests simultaneously; if
      they're downloading a page with images, for example.
      [color=blue]
      >Whilst a combination of connection_time out() and ignore_user_abo rt()
      >can stop the script from terminating when the client calls
      >termination, i'd like to terminate the clients connection rather than
      >having to wait for the client to do it.[/color]
      [color=blue]
      ><?
      > echo "Cleanup initiated";
      > return_control_ to_client_brows er_as_if_script _terminated();
      > run_verylong_du ll_cleanup_util ity_client_need _not_know_about ():
      >?>[/color]

      You may be better off scheduling your cleanup utility with cron rather
      than relying upon a client request to do it.

      --
      David ( @priz.co.uk )

      Comment

      • Geoff Berrow

        #4
        Re: Disconnecting client browser from php file

        I noticed that Message-ID: <c7g165$mif$1$8 30fa7a5@news.de mon.co.uk> from
        Tony Marston contained the following:
        [color=blue]
        >I think that your understanding of how server-side scripting languages such
        >as PHP work is seriously flawed. When a PHP script generates a web page
        >(which is an HTML document) it immediately terminates, so by the time the
        >client browser receives the page and starts to render it the PHP script has
        >already died.[/color]


        I used to think that but my experiences with odbc functions suggests
        otherwise.

        If I write

        <?php
        while($row = odbc_fetch_arra y($query)){

        ?>
        <table>
        <tr><td >Name:</td>
        <td><?=$row['fullname']?> </td></tr>
        ....
        </table>
        and right at the end

        }
        odbc_free_resul t($query);
        odbc_close($odb c);

        The script can time out on slow connections. However:

        <?php
        $code="";
        while($row = odbc_fetch_arra y($query)){

        $code.= " <tr> ";
        $code.= " <td>Name:</td>";
        $code.= " <td>";
        $code.= $row['fullname'];
        //snip intermediate code
        $code.= "</table>";
        }
        odbc_free_resul t($query);
        odbc_close($odb c);
        print $code;

        Works just fine. Now I know you're a clever guy Tony, can you explain
        this?
        --
        Geoff Berrow (put thecat out to email)
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        • Tony Marston

          #5
          Re: Disconnecting client browser from php file


          "Geoff Berrow" <blthecat@ckdog .co.uk> wrote in message
          news:ke8n90d93j p5rcv4fmr40l3nh sqjn1mfok@4ax.c om...[color=blue]
          > I noticed that Message-ID: <c7g165$mif$1$8 30fa7a5@news.de mon.co.uk> from
          > Tony Marston contained the following:
          >[color=green]
          > >I think that your understanding of how server-side scripting languages[/color][/color]
          such[color=blue][color=green]
          > >as PHP work is seriously flawed. When a PHP script generates a web page
          > >(which is an HTML document) it immediately terminates, so by the time the
          > >client browser receives the page and starts to render it the PHP script[/color][/color]
          has[color=blue][color=green]
          > >already died.[/color]
          >
          >
          > I used to think that but my experiences with odbc functions suggests
          > otherwise.
          >
          > If I write
          >
          > <?php
          > while($row = odbc_fetch_arra y($query)){
          >
          > ?>
          > <table>
          > <tr><td >Name:</td>
          > <td><?=$row['fullname']?> </td></tr>
          > ...
          > </table>
          > and right at the end
          >
          > }
          > odbc_free_resul t($query);
          > odbc_close($odb c);
          >
          > The script can time out on slow connections. However:
          >
          > <?php
          > $code="";
          > while($row = odbc_fetch_arra y($query)){
          >
          > $code.= " <tr> ";
          > $code.= " <td>Name:</td>";
          > $code.= " <td>";
          > $code.= $row['fullname'];
          > //snip intermediate code
          > $code.= "</table>";
          > }
          > odbc_free_resul t($query);
          > odbc_close($odb c);
          > print $code;
          >
          > Works just fine. Now I know you're a clever guy Tony, can you explain
          > this?[/color]

          I never use ODBC so I never have this problem.

          --
          Tony Marston

          This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




          [color=blue]
          > --
          > Geoff Berrow (put thecat out to email)
          > It's only Usenet, no one dies.
          > My opinions, not the committee's, mine.
          > Simple RFDs http://www.ckdog.co.uk/rfdmaker/[/color]


          Comment

          • Geoff Berrow

            #6
            Re: Disconnecting client browser from php file

            I noticed that Message-ID: <c7gh1n$ips$1$8 302bc10@news.de mon.co.uk> from
            Tony Marston contained the following:
            [color=blue][color=green]
            >>Now I know you're a clever guy Tony, can you explain
            >> this?[/color]
            >
            >I never use ODBC so I never have this problem.[/color]

            Pity, I was hoping for an insight.

            Do you not agree that, on the face of it, it negates what you said?

            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            • Daniel Tryba

              #7
              Re: Disconnecting client browser from php file

              Tony Marston <tony@nospam.de mon.co.uk> wrote:[color=blue]
              > I think that your understanding of how server-side scripting languages such
              > as PHP work is seriously flawed. When a PHP script generates a web page
              > (which is an HTML document) it immediately terminates, so by the time the
              > client browser receives the page and starts to render it the PHP script has
              > already died.[/color]

              This is false. You might experience this kind of behavior when output
              buffering is on (either in PHP or in the webserver itself).

              BTW http/1.1 has a special feature to make "streaming" in http more
              easy: chunked transfer encoding.

              --

              Daniel Tryba

              Comment

              • Timothy Madden

                #8
                Re: Disconnecting client browser from php file

                chris wrote:
                [color=blue]
                > I'd like to initiate a php script using a standard httpd request, but
                > then allow the client browser to disconnect without terminating the
                > script. Freeing up the browser to access other functions on the site.[/color]

                I don't know how to do exactly what you asked, but have you tried to

                echo "\n</BODY>\n";

                and then output nothing more from your script ? This will have the browser
                render the page and then wait. This is what I did when I had to update site
                statistics after each succesfull login of a memeber

                --
                Timothy Madden
                Romania
                -------------------------
                And I don't wanna miss a thing

                Comment

                • Timothy Madden

                  #9
                  Re: Disconnecting client browser from php file

                  chris wrote:
                  [color=blue]
                  > I'd like to initiate a php script using a standard httpd request, but
                  > then allow the client browser to disconnect without terminating the
                  > script. Freeing up the browser to access other functions on the site.
                  >[/color]
                  I think what you're looking for is register_shutdo wn_function()
                  Searc at http://www.php.net/manual/en
                  --
                  Timothy Madden
                  Romania
                  -------------------------
                  And I don't wanna miss a thing

                  Comment

                  • Tony Marston

                    #10
                    Re: Disconnecting client browser from php file


                    "Geoff Berrow" <blthecat@ckdog .co.uk> wrote in message
                    news:lcin90959c dai6ffear133nvn f5feq5ltq@4ax.c om...[color=blue]
                    > I noticed that Message-ID: <c7gh1n$ips$1$8 302bc10@news.de mon.co.uk> from
                    > Tony Marston contained the following:
                    >[color=green][color=darkred]
                    > >>Now I know you're a clever guy Tony, can you explain
                    > >> this?[/color]
                    > >
                    > >I never use ODBC so I never have this problem.[/color]
                    >
                    > Pity, I was hoping for an insight.
                    >
                    > Do you not agree that, on the face of it, it negates what you said?[/color]

                    No. The HTML output generated by your PHP script is not transmitted to the
                    client in bits and pieces as it is being generated, it is sent in one burst.

                    --
                    Tony Marston

                    This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL



                    [color=blue]
                    > --
                    > Geoff Berrow (put thecat out to email)
                    > It's only Usenet, no one dies.
                    > My opinions, not the committee's, mine.
                    > Simple RFDs http://www.ckdog.co.uk/rfdmaker/[/color]


                    Comment

                    • Andy Hassall

                      #11
                      Re: Disconnecting client browser from php file

                      On Sat, 8 May 2004 00:33:58 +0100, "Tony Marston" <tony@NOSPAM.de mon.co.uk>
                      wrote:
                      [color=blue]
                      >No. The HTML output generated by your PHP script is not transmitted to the
                      >client in bits and pieces as it is being generated, it is sent in one burst.[/color]

                      Provably false...

                      <pre>
                      <?php
                      set_time_limit( 0);

                      for ($i=0; $i<20; $i++)
                      {
                      echo str_repeat('x', 5000);
                      echo "\nend of batch\n\n";
                      sleep(2);
                      }

                      ?>
                      </pre>

                      Do you begin to see output after:

                      (a) 40 seconds - output is not sent until script terminates.
                      (b) <40 seconds - output is sent during execution of script.

                      I get (b) - output appears in chunks, starting a couple of seconds after the
                      request.

                      Unless you meant something else. But it doesn't look like it, from your other
                      statement:
                      [color=blue]
                      >When a PHP script generates a web page
                      >(which is an HTML document) it immediately terminates, so by the time the
                      >client browser receives the page and starts to render it the PHP script has
                      >already died.[/color]

                      That one would only be true for very short scripts and/or high latency
                      networks.

                      --
                      Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                      http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

                      Comment

                      • Daniel Tryba

                        #12
                        Re: Disconnecting client browser from php file

                        Andy Hassall <andy@andyh.co. uk> wrote:[color=blue]
                        > <?php
                        > set_time_limit( 0);
                        >
                        > for ($i=0; $i<20; $i++)
                        > {
                        > echo str_repeat('x', 5000);
                        > echo "\nend of batch\n\n";[/color]
                        flush();[color=blue]
                        > sleep(2);
                        > }
                        > ?>
                        > </pre>
                        >
                        > Do you begin to see output after:
                        >
                        > (a) 40 seconds - output is not sent until script terminates.
                        > (b) <40 seconds - output is sent during execution of script.
                        >
                        > I get (b) - output appears in chunks, starting a couple of seconds after the
                        > request.[/color]

                        It all depends on buffering. Without flush() a might be true in most
                        setups.

                        --

                        Daniel Tryba

                        Comment

                        • Tony Marston

                          #13
                          Re: Disconnecting client browser from php file


                          "Andy Hassall" <andy@andyh.co. uk> wrote in message
                          news:f88o90t9i7 qo2s741jkh21kes bgs53lq9l@4ax.c om...[color=blue]
                          > On Sat, 8 May 2004 00:33:58 +0100, "Tony Marston"[/color]
                          <tony@NOSPAM.de mon.co.uk>[color=blue]
                          > wrote:
                          >[color=green]
                          > >No. The HTML output generated by your PHP script is not transmitted to[/color][/color]
                          the[color=blue][color=green]
                          > >client in bits and pieces as it is being generated, it is sent in one[/color][/color]
                          burst.[color=blue]
                          >
                          > Provably false...
                          >
                          > <pre>
                          > <?php
                          > set_time_limit( 0);
                          >
                          > for ($i=0; $i<20; $i++)
                          > {
                          > echo str_repeat('x', 5000);
                          > echo "\nend of batch\n\n";
                          > sleep(2);
                          > }
                          >
                          > ?>
                          > </pre>
                          >
                          > Do you begin to see output after:
                          >
                          > (a) 40 seconds - output is not sent until script terminates.
                          > (b) <40 seconds - output is sent during execution of script.
                          >
                          > I get (b) - output appears in chunks, starting a couple of seconds after[/color]
                          the[color=blue]
                          > request.
                          >
                          > Unless you meant something else. But it doesn't look like it, from your[/color]
                          other[color=blue]
                          > statement:[/color]

                          I do not generate very large HTML documents so I have never hit this
                          situation. AFAIK the output from a PHP script is not sent to the client
                          browser until the output stream is closed, which is usually when the script
                          terminates (unless you are using output buffering). I have certainly never
                          observed the situation where a single line is sent out as soon as it has
                          been written. It may be possible that once the output stream hits a certain
                          limit that chunk is sent while the next chunk is being built, but I have
                          never seen any documentation to that effect.

                          --
                          Tony Marston
                          This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL



                          [color=blue][color=green]
                          > >When a PHP script generates a web page
                          > >(which is an HTML document) it immediately terminates, so by the time the
                          > >client browser receives the page and starts to render it the PHP script[/color][/color]
                          has[color=blue][color=green]
                          > >already died.[/color]
                          >
                          > That one would only be true for very short scripts and/or high latency
                          > networks.
                          >
                          > --
                          > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                          > http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space[/color]


                          Comment

                          • Geoff Berrow

                            #14
                            Re: Disconnecting client browser from php file

                            I noticed that Message-ID: <c7i8hg$a2k$1$8 300dec7@news.de mon.co.uk> from
                            Tony Marston contained the following:
                            [color=blue]
                            >I do not generate very large HTML documents so I have never hit this
                            >situation. AFAIK the output from a PHP script is not sent to the client
                            >browser until the output stream is closed, which is usually when the script
                            >terminates (unless you are using output buffering). I have certainly never
                            >observed the situation where a single line is sent out as soon as it has
                            >been written. It may be possible that once the output stream hits a certain
                            >limit that chunk is sent while the next chunk is being built, but I have
                            >never seen any documentation to that effect.[/color]

                            Well, there's me telling you it happens with my odbc script and the
                            client with the slow connection and there's a bit of code demonstrating
                            it. I'm just wondering what it will take for you to admit it might
                            actually be true.

                            FWIW, enabling output buffering ( I did it by using ob_start() -
                            ob_end_flush() ) cures the problem.

                            --
                            Geoff Berrow (put thecat out to email)
                            It's only Usenet, no one dies.
                            My opinions, not the committee's, mine.
                            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                            Comment

                            • Andy Hassall

                              #15
                              Re: Disconnecting client browser from php file

                              On Sat, 8 May 2004 10:16:30 +0100, "Tony Marston" <tony@NOSPAM.de mon.co.uk>
                              wrote:
                              [color=blue]
                              >I do not generate very large HTML documents so I have never hit this
                              >situation. AFAIK the output from a PHP script is not sent to the client
                              >browser until the output stream is closed, which is usually when the script
                              >terminates (unless you are using output buffering). I have certainly never
                              >observed the situation where a single line is sent out as soon as it has
                              >been written. It may be possible that once the output stream hits a certain
                              >limit that chunk is sent while the next chunk is being built, but I have
                              >never seen any documentation to that effect.[/color]

                              The flush() manual page has some hints towards this:



                              There _are_ configurations where output is deferred until after the script
                              completes, but under the default configuration of:

                              - output_bufferin g=Off
                              - no gzip compression either by PHP or Apache
                              - a Unix-based OS

                              ... output is definitely sent _during_ the execution of the script if you send
                              more than a couple of k of data, even without calling flush().

                              --
                              Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                              http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

                              Comment

                              Working...