473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2822

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sc0ri0n | last post by:
Hi, I want to be able to tail -f log files on a different machine and display the results in a web page. Below code from the cgi works: $cmd = "rsh -l $acc $Host tail -f $LogFileName |";...
18
by: cjl | last post by:
Hey all: I know that it is silly in the age of Google to 'lose' something on the internet, but I recently checked out a project that had implemented a database with a subset of SQL in pure...
5
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an...
7
by: Andreas Håkansson | last post by:
Hello, I'm building a small TCP based server which uses the Async commands. When a new connection is made to the server, I store the new socket in a hashtable, using the client IP as the key. ...
7
by: moondaddy | last post by:
I want to dynamically create a JavaScript file and cache it on the client for re-use. I know how to write javascript to a web page from the code behind, but I don't know how to actually create a...
8
by: DanB | last post by:
This is probably soooo simple but I can't seem to get it. I have a text file that I want users to download via a web page. I want the file to be saved to a default folder (or one that they...
13
by: Joner | last post by:
Hello, I'm having trouble with a little programme of mine where I connect to an access database. It seems to connect fine, and disconnect fine, but then after it won't reconnect, I get the error...
6
by: dinoo | last post by:
Hi, I would appreciate if some one could help me out. I have to read a client side ini file in Aspx page and use that data in server side processing. Can any one help me out here? Please refer...
3
by: =?Utf-8?B?UmludSBHb3BhbGFrcmlzaG5hIFBpbGxhaQ==?= | last post by:
Hi All, I have a ASP/C# application that connect to Oracle database . After issuing my SQL query if I close the browser or move into another page ( ie whle executing in the databse serevr) will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.