469,942 Members | 2,550 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,942 developers. It's quick & easy.

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_timeout() and ignore_user_abort()
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_browser_as_if_script_term inated();
run_verylong_dull_cleanup_utility_client_need_not_ know_about():
?>
Jul 17 '05 #1
18 2585

"chris" <ch***@remotegoat.co.uk> wrote in message
news:10**************************@posting.google.c om...
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.
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
http://www.tonymarston.net

Whilst a combination of connection_timeout() and ignore_user_abort()
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_browser_as_if_script_term inated();
run_verylong_dull_cleanup_utility_client_need_not_ know_about():
?>

Jul 17 '05 #2
On 7 May 2004 05:01:33 -0700, ch***@remotegoat.co.uk (chris) wrote:
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.
Browsers will often handle multiple http requests simultaneously; if
they're downloading a page with images, for example.
Whilst a combination of connection_timeout() and ignore_user_abort()
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. <?
echo "Cleanup initiated";
return_control_to_client_browser_as_if_script_term inated();
run_verylong_dull_cleanup_utility_client_need_not_ know_about():
?>


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 )
Jul 17 '05 #3
I noticed that Message-ID: <c7*******************@news.demon.co.uk> from
Tony Marston contained the following:
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.

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

If I write

<?php
while($row = odbc_fetch_array($query)){

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

}
odbc_free_result($query);
odbc_close($odbc);

The script can time out on slow connections. However:

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

$code.= " <tr> ";
$code.= " <td>Name:</td>";
$code.= " <td>";
$code.= $row['fullname'];
//snip intermediate code
$code.= "</table>";
}
odbc_free_result($query);
odbc_close($odbc);
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/
Jul 17 '05 #4

"Geoff Berrow" <bl******@ckdog.co.uk> wrote in message
news:ke********************************@4ax.com...
I noticed that Message-ID: <c7*******************@news.demon.co.uk> from
Tony Marston contained the following:
I think that your understanding of how server-side scripting languages suchas 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 hasalready died.

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

If I write

<?php
while($row = odbc_fetch_array($query)){

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

}
odbc_free_result($query);
odbc_close($odbc);

The script can time out on slow connections. However:

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

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

Works just fine. Now I know you're a clever guy Tony, can you explain
this?


I never use ODBC so I never have this problem.

--
Tony Marston

http://www.tonymarston.net

--
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/

Jul 17 '05 #5
I noticed that Message-ID: <c7*******************@news.demon.co.uk> from
Tony Marston contained the following:
Now I know you're a clever guy Tony, can you explain
this?


I never use ODBC so I never have this problem.


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/
Jul 17 '05 #6
Tony Marston <to**@nospam.demon.co.uk> wrote:
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.


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

Jul 17 '05 #7
chris wrote:
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.


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
Jul 17 '05 #8
chris wrote:
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.

I think what you're looking for is register_shutdown_function()
Searc at http://www.php.net/manual/en
--
Timothy Madden
Romania
-------------------------
And I don't wanna miss a thing
Jul 17 '05 #9

"Geoff Berrow" <bl******@ckdog.co.uk> wrote in message
news:lc********************************@4ax.com...
I noticed that Message-ID: <c7*******************@news.demon.co.uk> from
Tony Marston contained the following:
Now I know you're a clever guy Tony, can you explain
this?
I never use ODBC so I never have this problem.


Pity, I was hoping for an insight.

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


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

http://www.tonymarston.net
--
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/

Jul 17 '05 #10
On Sat, 8 May 2004 00:33:58 +0100, "Tony Marston" <to**@NOSPAM.demon.co.uk>
wrote:
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.
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:
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.


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

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #11
Andy Hassall <an**@andyh.co.uk> wrote:
<?php
set_time_limit(0);

for ($i=0; $i<20; $i++)
{
echo str_repeat('x', 5000);
echo "\nend of batch\n\n"; flush(); 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.


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

--

Daniel Tryba

Jul 17 '05 #12

"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:f8********************************@4ax.com...
On Sat, 8 May 2004 00:33:58 +0100, "Tony Marston" <to**@NOSPAM.demon.co.uk> wrote:
No. The HTML output generated by your PHP script is not transmitted to theclient in bits and pieces as it is being generated, it is sent in one burst.

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:


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
http://www.tonymarston.net
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 hasalready died.


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

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

Jul 17 '05 #13
I noticed that Message-ID: <c7*******************@news.demon.co.uk> from
Tony Marston contained the following:
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.


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/
Jul 17 '05 #14
On Sat, 8 May 2004 10:16:30 +0100, "Tony Marston" <to**@NOSPAM.demon.co.uk>
wrote:
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.


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

http://uk2.php.net/flush

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

- output_buffering=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 <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #15
I noticed that Message-ID: <4d********************************@4ax.com>
from Andy Hassall contained the following:
There _are_ configurations where output is deferred until after the script
completes, but under the default configuration of:

- output_buffering=Off


What happens if output_buffering is set to no value?

--
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/
Jul 17 '05 #16
On Sat, 08 May 2004 13:08:11 +0100, Geoff Berrow <bl******@ckdog.co.uk> wrote:
I noticed that Message-ID: <4d********************************@4ax.com>
from Andy Hassall contained the following:
There _are_ configurations where output is deferred until after the script
completes, but under the default configuration of:

- output_buffering=Off


What happens if output_buffering is set to no value?


Have just tried it, and It acts as if it were set to Off.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #17

"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:4d********************************@4ax.com...
On Sat, 8 May 2004 10:16:30 +0100, "Tony Marston" <to**@NOSPAM.demon.co.uk> wrote:
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 scriptterminates (unless you are using output buffering). I have certainly neverobserved 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 certainlimit that chunk is sent while the next chunk is being built, but I have
never seen any documentation to that effect.
The flush() manual page has some hints towards this:

http://uk2.php.net/flush

There _are_ configurations where output is deferred until after the

script completes, but under the default configuration of:

- output_buffering=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().
It appears that the sending of output can be affected by several variables -
the size of your output data, your web server, the client browser. So in
some cases nothing is sent until the script terminates, while in others it
may be sent in chunks before the script terminates. You learn something new
every day.

--
Tony Marston
http://www.tonymarston.net
--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

Jul 17 '05 #18
"chris" <ch***@remotegoat.co.uk> wrote in message
news:10**************************@posting.google.c om...
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_timeout() and ignore_user_abort()
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_browser_as_if_script_term inated();
run_verylong_dull_cleanup_utility_client_need_not_ know_about():
?>


Another possibility is to spawn another instance of PHP by making a local
HTTP request.

<?
echo "Cleanup initiated";

fopen(http://localhost/verylong_dull_clean...ed_not_know_ab
out.php, "r");
?>

The clean up script would need to ignore user abort, of course.
Jul 17 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by sc0ri0n | last post: by
7 posts views Thread by Andreas Håkansson | last post: by
6 posts views Thread by dinoo | last post: by
3 posts views Thread by =?Utf-8?B?UmludSBHb3BhbGFrcmlzaG5hIFBpbGxhaQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.