note 71172 added to features.connection-handling
| From: | arr1 at hotmail dot co dot uk | Date: | Tue, 14 Nov 2006 19:51:47 +0000 |
| Subject: | note 71172 added to features.connection-handling | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119732@lists.php.net to get a copy of this message | ||
Closing the users browser connection whilst keeping your php script running has been an issue since
4.1, when the behaviour of register_shutdown_function() was modified so that it would not
automatically close the users connection.
sts at mail dot xubion dot hu
Posted the original solution:
<?php
header("Connection: close");
ob_start();
phpinfo();
$size=ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
sleep(13);
error_log("do something in the background");
?>
Which works fine until you substitute phpinfo() for
echo ('text I want user to see'); in which case the headers are never sent!
The solution is to explicitly turn off output buffering and clear the buffer prior to sending your
header information.
example:
<?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');
?>
Just spent 3 hours trying to figure this one out, hope it helps someone :)
Tested in:
IE 7.5730.11
Mozilla Firefox 1.81
----
Server IP: 85.116.0.130
Probable Submitter: 139.184.30.16 (proxied: 139.184.134.166)
----
Manual Page -- http://www.php.net/manual/en/features.connection-handling.php
Edit -- https://master.php.net/note/edit/71172
Del: integrated -- https://master.php.net/note/delete/71172/integrated
Del: useless -- https://master.php.net/note/delete/71172/useless
Del: bad code -- https://master.php.net/note/delete/71172/bad+code
Del: spam -- https://master.php.net/note/delete/71172/spam
Del: non-english -- https://master.php.net/note/delete/71172/non-english
Del: in docs -- https://master.php.net/note/delete/71172/in+docs
Del: other reasons-- https://master.php.net/note/delete/71172
Reject -- https://master.php.net/note/reject/71172
Search -- https://master.php.net/manage/user-notes.php