Terminate http stream.

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Terminate http stream.

Post by Benjamin »

I have been digging through the manual looking for a way to force a browser to stop waiting for data. See the following code..

Code: Select all

echo 'Hello Browser';

// insert command to tell browser no more data is coming..

// do some other stuff
sleep(10);
As it is, the page won't load for 10 seconds. What possible solutions are there to this issue?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Spoke too soon.. Found this in the manual..

Code: Select all

<?php
 ob_end_clean();
 header("Connection: close");
 ignore_user_abort(); // optional
 ob_start();
 echo ('Text the user will see');
 $size = ob_get_length();
 header("Content-Length: $size");
 ob_end_flush(); // Strange behaviour, will not work
 flush();            // Unless both are called !
 // Do processing here
 sleep(30);
 echo('Text user will never see');
?>
Chapter 40. Connection handling

Looks like a pretty crappy way to do it though. Anyone have anything better?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Looks like a pretty crappy way to do it though.
What's wrong with that?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

1. I'm thinking that there should be a native PHP function which will close the connection without terminating the script.
2. I don't know enough about how browsers react to headers to be sure that this will work in all browsers.
3. It's not pretty, I prefer one liners.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Actually that doesn't work too well with IE. If there are images that need to be loaded IE will still sit there and wait for the images before it actually draws the page. Nothing you can do from the server side to stop that.
Post Reply