Π Π°Π±ΠΎΡ‚Π° с соСдинСниями

Бтатус соСдинСния сохраняСтся Π²Π½ΡƒΡ‚Ρ€Π΅Π½Π½ΠΈΠΌΠΈ ΠΌΠ΅Ρ…Π°Π½ΠΈΠ·ΠΌΠ°ΠΌΠΈ PHP. НиТС пСрСчислСны Ρ‡Π΅Ρ‚Ρ‹Ρ€Π΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ‹Π΅ состояния:

  • 0 - NORMAL
  • 1 - ABORTED
  • 2 - TIMEOUT
  • 3 - ABORTED ΠΈ TIMEOUT

Π’ΠΎ врСмя ΡˆΡ‚Π°Ρ‚Π½ΠΎΠ³ΠΎ выполнСния PHP-скрипта установлСн статус NORMAL. Π’ случаС, Ссли ΡƒΠ΄Π°Π»Ρ‘Π½Π½Ρ‹ΠΉ ΠΊΠ»ΠΈΠ΅Π½Ρ‚ Ρ€Π°Π·ΠΎΡ€Π²Π°Π» соСдинСниС, статус измСняСтся Π½Π° ABORTED. Π§Π°Ρ‰Π΅ всСго отсоСдинСниС ΡƒΠ΄Π°Π»Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΊΠ»ΠΈΠ΅Π½Ρ‚Π° происходит ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ ΠΊΠ½ΠΎΠΏΠΊΠΈ "Stop" Π² Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π΅. Π’ случаС, Ссли достигаСтся установлСнный Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ Π»ΠΈΠΌΠΈΡ‚ (ΠΎΠ·Π½Π°ΠΊΠΎΠΌΡŒΡ‚Π΅ΡΡŒ с Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ set_time_limit()), Π±ΡƒΠ΄Π΅Ρ‚ установлСн статус TIMEOUT.

Π’Ρ‹ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ Ρ€Π΅ΡˆΠ°Ρ‚ΡŒ, ΠΏΡ€ΠΈΠ²ΠΎΠ΄ΠΈΡ‚ Π»ΠΈ ΠΎΡ‚ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ ΠΊΠ»ΠΈΠ΅Π½Ρ‚Π° ΠΊ Π·Π°Π²Π΅Ρ€ΡˆΠ΅Π½ΠΈΡŽ вашСго скрипта. Иногда Π±Ρ‹Π²Π°Π΅Ρ‚ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ скрипт выполнился Π΄ΠΎ ΠΊΠΎΠ½Ρ†Π°, Π΄Π°ΠΆΠ΅ Ссли отсутствуСт ΡƒΠ΄Π°Π»Ρ‘Π½Π½Ρ‹ΠΉ Π±Ρ€Π°ΡƒΠ·Π΅Ρ€, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ адрСсован вСсь Π²Ρ‹Π²ΠΎΠ΄. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ скрипт Π·Π°Π²Π΅Ρ€ΡˆΠ°Π΅Ρ‚ свою Ρ€Π°Π±ΠΎΡ‚Ρƒ ΠΏΡ€ΠΈ ΠΎΡ‚ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ‚Π°. Π­Ρ‚ΠΎ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΎ ΠΏΡ€ΠΈ ΠΏΠΎΠΌΠΎΡ‰ΠΈ ΠΎΠΏΡ†ΠΈΠΈ ignore_user_abort Π² ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ†ΠΈΠΎΠ½Π½ΠΎΠΌ Ρ„Π°ΠΉΠ»Π΅ php.ini. Π’Π°ΠΊΠΎΠ³ΠΎ ΠΆΠ΅ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π° ΠΌΠΎΠΆΠ½ΠΎ Π΄ΠΎΠ±ΠΈΡ‚ΡŒΡΡ, ΡƒΠΊΠ°Π·Π°Π² php_value ignore_user_abort Π² ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ†ΠΈΠΎΠ½Π½ΠΎΠΌ Ρ„Π°ΠΉΠ»Π΅ Apache httpd.conf ΠΈΠ»ΠΈ воспользовавшись Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ ignore_user_abort(). Если Π²Ρ‹ явно Π½Π΅ ΡƒΠΊΠ°Π·Π°Π»ΠΈ Π½Π° Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ ΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ Ρ€Π°Π·Ρ€Ρ‹Π² соСдинСния с ΠΊΠ»ΠΈΠ΅Π½Ρ‚ΠΎΠΌ, Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ скрипта Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΡ€Π΅Ρ€Π²Π°Π½ΠΎ. Π˜ΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ΠΌ являСтся Ρ‚ΠΎΡ‚ случай, Ссли ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ register_shutdown_function(), Π²Ρ‹ ΡƒΠΊΠ°Π·Π°Π»ΠΈ ΡΠΏΠ΅Ρ†ΠΈΠ°Π»ΡŒΠ½ΡƒΡŽ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ, Π²Ρ‹Π·Ρ‹Π²Π°Π΅ΠΌΡƒΡŽ ΠΏΡ€ΠΈ Π·Π°Π²Π΅Ρ€ΡˆΠ΅Π½ΠΈΠΈ скрипта. Π’ Ρ‚Π°ΠΊΠΎΠΌ случаС послС Ρ‚ΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒ Π½Π°ΠΆΠ°Π» ΠΊΠ½ΠΎΠΏΠΊΡƒ "Stop" Π² своём Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π΅, ΠΏΡ€ΠΈ ΠΏΠ΅Ρ€Π²ΠΎΠΉ ΠΆΠ΅ ΠΏΠΎΠΏΡ‹Ρ‚ΠΊΠ΅ Ρ‡Ρ‚ΠΎ-Π»ΠΈΠ±ΠΎ вывСсти PHP ΠΎΠ±Π½Π°Ρ€ΡƒΠΆΠΈΠ²Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ соСдинСниС с ΠΊΠ»ΠΈΠ΅Π½Ρ‚ΠΎΠΌ Π±Ρ‹Π»ΠΎ утСряно, ΠΈ Π²Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ Π·Π°Π²Π΅Ρ€ΡˆΠ°ΡŽΡ‰ΡƒΡŽ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ. Π­Ρ‚Π° функция Ρ‚Π°ΠΊΠΆΠ΅ вызываСтся ΠΏΡ€ΠΈ Π½ΠΎΡ€ΠΌΠ°Π»ΡŒΠ½ΠΎΠΌ Π·Π°Π²Π΅Ρ€ΡˆΠ΅Π½ΠΈΠΈ Ρ€Π°Π±ΠΎΡ‚Ρ‹ вашСго скрипта, поэтому для Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ спСцифичСскиС дСйствия ΠΏΡ€ΠΈ отсоСдинСнии ΠΊΠ»ΠΈΠ΅Π½Ρ‚Π°, Π²Π°ΠΌ понадобится функция connection_aborted(), которая Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли соСдинСниС Π±Ρ‹Π»ΠΎ Ρ€Π°Π·ΠΎΡ€Π²Π°Π½ΠΎ.

Π’Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ вашСго скрипта Ρ‚Π°ΠΊΠΆΠ΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΏΡ€Π΅Ρ€Π²Π°Π½ΠΎ встроСнным Ρ‚Π°ΠΉΠΌΠ΅Ρ€ΠΎΠΌ. Π‘Ρ‚Π°Π½Π΄Π°Ρ€Ρ‚Π½ΠΎΠ΅ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ составляСт 30 сСкунд, ΠΈΠ·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π΅Π³ΠΎ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡ€ΠΈ ΠΏΠΎΠΌΠΎΡ‰ΠΈ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΈΠ²Ρ‹ max_execution_time Π² ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ†ΠΈΠΎΠ½Π½ΠΎΠΌ Ρ„Π°ΠΉΠ»Π΅ php.ini. Π’Π°ΠΊΠΎΠ³ΠΎ ΠΆΠ΅ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π° ΠΌΠΎΠΆΠ½ΠΎ Π΄ΠΎΡΡ‚ΠΈΡ‡ΡŒ, Π΄ΠΎΠ±Π°Π²ΠΈΠ² php_value max_execution_time Π² ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ†ΠΈΠΎΠ½Π½Ρ‹ΠΉ Ρ„Π°ΠΉΠ» Apache httpd.conf ΠΈΠ»ΠΈ воспользовавшись Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ set_time_limit(). ΠŸΡ€ΠΈ достиТСнии скриптом Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠ³ΠΎ Π»ΠΈΠΌΠΈΡ‚Π° Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ скрипта прСрываСтся ΠΈ вызываСтся Π·Π°Π²Π΅Ρ€ΡˆΠ°ΡŽΡ‰Π°Ρ функция, Ссли ΠΎΠ½Π° Π±Ρ‹Π»Π° ΡƒΠΊΠ°Π·Π°Π½Π°. Π£Ρ‚ΠΎΡ‡Π½ΠΈΡ‚ΡŒ ΠΏΡ€ΠΈΡ‡ΠΈΠ½Ρƒ Π·Π°Π²Π΅Ρ€ΡˆΠ΅Π½ΠΈΡ скрипта Π²Ρ‹ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ ΠΏΡ€ΠΈ ΠΏΠΎΠΌΠΎΡ‰ΠΈ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ connection_status(), которая Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ 2, Ссли скрипт Π±Ρ‹Π» ΠΏΡ€Π΅Ρ€Π²Π°Π½ ΠΏΠΎ Π΄ΠΎΡΡ‚ΠΈΠΆΠ΅Π½ΠΈΡŽ Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠ³ΠΎ ограничСния.

ЕдинствСнноС, Ρ‡Ρ‚ΠΎ слСдуСт Π·Π°ΠΌΠ΅Ρ‚ΠΈΡ‚ΡŒ - Ρ‡Ρ‚ΠΎ ΠΎΠ±Π° статуса: ABORTED ΠΈ TIMEOUT, - ΠΌΠΎΠ³ΡƒΡ‚ Π±Ρ‹Ρ‚ΡŒ установлСны ΠΎΠ΄Π½ΠΎΠ²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎ. Π­Ρ‚ΠΎ ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΡ€ΠΎΠΈΠ·ΠΎΠΉΡ‚ΠΈ Π² Ρ‚ΠΎΠΌ случаС, Ссли Π²Ρ‹ явно ΡƒΠΊΠ°Π·Π°Π»ΠΈ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΡΡ‚ΡŒ ΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ отсоСдинСниС ΡƒΠ΄Π°Π»Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΊΠ»ΠΈΠ΅Π½Ρ‚Π°. Π’ Ρ‚Π°ΠΊΠΎΠΌ случаС послС Ρ€Π°Π·Ρ€Ρ‹Π²Π° соСдинСния, ΠΎΡ‚ΠΌΠ΅Ρ‚ΠΈΠ² этот Ρ„Π°ΠΊΡ‚, PHP ΠΏΡ€ΠΎΠ΄ΠΎΠ»ΠΆΠΈΡ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ скрипта, ΠΈ ΠΏΡ€ΠΈ достиТСнии Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠ³ΠΎ Π»ΠΈΠΌΠΈΡ‚Π° Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π·Π²Π°Π½Π° Π·Π°Π²Π΅Ρ€ΡˆΠ°ΡŽΡ‰Π°Ρ функция, Ссли таковая Π±Ρ‹Π»Π° ΡƒΠΊΠ°Π·Π°Π½Π°. Π’ этой Ρ‚ΠΎΡ‡ΠΊΠ΅ Π²Ρ‹ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ ΠΎΠ±Π½Π°Ρ€ΡƒΠΆΠΈΡ‚ΡŒ, Ρ‡Ρ‚ΠΎ connection_status() Π²Π΅Ρ€Π½Ρ‘Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 3.

οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 11 notes

up
67
tom lgold2003 at gmail dot com ΒΆ
16 years ago
hey, thanks to arr1, and it is very useful for me, when I need to return to the user fast and then do something else.

When using the codes, it nearly drive me mad and I found another thing that may affect the codes:

Content-Encoding: gzip

This is because the zlib is on and the content will be compressed. But this will not output the buffer until all output is over.

So, it may need to send the header to prevent this problem.

now, the code becomes:

<?php
ob_end_clean();
header("Connection: close\r\n");
header("Content-Encoding: none\r\n");
ignore_user_abort(true); // optional
ob_start();
echo ('Text user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();     // Strange behaviour, will not work
flush();            // Unless both are called !
ob_end_clean();

//do processing here
sleep(5);

echo('Text user will never see');
//do some processing
?>
up
55
arr1 at hotmail dot co dot uk ΒΆ
19 years ago
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
up
40
mheumann at comciencia dot cl ΒΆ
12 years ago
I had a lot of problems getting a redirect to work, after which my script was intended to keep working in the background. The redirect to another page of my site simply would only work once the original page had finished processing.

I finally found out what was wrong:
The session only gets closed by PHP at the very end of the script, and since access to the session data is locked to prevent more than one page writing to it simultaneously, the new page cannot load until the original processing has finished.

Solution:
Close the session manually when redirecting using session_write_close():

<?php
ignore_user_abort(true);
set_time_limit(0);

$strURL = "PUT YOUR REDIRCT HERE";
header("Location: $strURL", true);
header("Connection: close", true);
header("Content-Encoding: none\r\n");
header("Content-Length: 0", true);

flush();
ob_flush();

session_write_close();

// Continue processing...

sleep(100);
exit;
?>

But careful:
Make sure that your script doesn't write to the session after session_write_close(), i.e. in your background processing code.  That won't work.  Also avoid reading, remember, the next script may already have modified the data.

So try to read out the data you need prior to redirecting.
up
32
Lee ΒΆ
21 years ago
The point mentioned in the last comment isn't always the case.

If a user's connection is lost half way through an order processing script is confirming a user's credit card/adding them to a DB, etc (due to their ISP going down, network trouble... whatever) and your script tries to send back output (such as, "pre-processing order" or any other type of confirmation), then your script will abort -- and this could cause problems for your process.

I have an order script that adds data to a InnoDB database (through MySQL) and only commits the transactions upon successful completion. Without ignore_user_abort(), I have had times when a user's connection dropped during the processing phase... and their card was charged, but they weren't added to my local DB.

So, it's always safe to ignore any aborts if you are processing sensitive transactions that should go ahead, whether your user is "watching" on the other end or not.
up
23
Marco ΒΆ
9 years ago
The CONNECTION_XXX constants that are not listed here for some reason are:

0 = CONNECTION_NORMAL
1 = CONNECTION_ABORTED
2 = CONNECTION_TIMEOUT
3 = CONNECTION_ABORTED & CONNECTION_TIMEOUT

Number 3 is effectively tested like this:
if (CONNECTION_ABORTED & CONNECTION_TIMEOUT)
    echo 'Connection both aborted and timed out';
up
28
a1n2ton at gmail dot com ΒΆ
16 years ago
PHP changes directory on connection abort so code like this will not do what you want:

<?php
function abort()
{
     if(connection_aborted())
           unlink('file.ini');
}
register_shutdown_function('abort');
?>

actually it will delete file in apaches's root dir so if you want to unlink file in your script's dir on abort or write to it you have to store directory
<?php
function abort()
{
     global $dsd;
     if(connection_aborted())
           unlink($dsd.'/file.ini');
}
register_shutdown_function('abort');
$dsd=getcwd();
?>
up
27
Ilya Penyaev ΒΆ
13 years ago
I was quite stuck when trying to make my script redirect the client to another URL and then continue processing. The reason was php-fpm. All possible buffer flushes did not work, unless I called fastcgi_finish_request();

For example:

<?php
    // redirecting...
    ignore_user_abort(true);
    header("Location: ".$redirectUrl, true);
    header("Connection: close", true);
    header("Content-Length: 0", true);
    ob_end_flush();
    flush();
    fastcgi_finish_request(); // important when using php-fpm!
    
    sleep (5); // User won't feel this sleep because he'll already be away
    
    // do some work after user has been redirected
?>
up
24
Anonymous ΒΆ
14 years ago
This simple function outputs a string and closes the connection. It considers compression using "ob_gzhandler"

It took me a little while to put this all together, mostly because setting the encoding to none, as some people noted here, didn't work.

<?php
function outputStringAndCloseConnection2($stringToOutput)
{    
    set_time_limit(0);
    ignore_user_abort(true);    
    // buffer all upcoming output - make sure we care about compression:
    if(!ob_start("ob_gzhandler"))
        ob_start();         
    echo $stringToOutput;    
    // get the size of the output
    $size = ob_get_length();    
    // send headers to tell the browser to close the connection    
    header("Content-Length: $size");
    header('Connection: close');    
    // flush all output
    ob_end_flush();
    ob_flush();
    flush();    
    // close current session
    if (session_id()) session_write_close();
}
?>
up
22
Anonymous ΒΆ
18 years ago
in regards of posting from:
arr1 at hotmail dot co dot uk

if you use/write sessions you need to do this before:
(otherwise it does not work)

session_write_close();

and if wanted:

ignore_user_abort(TRUE);
instead of ignore_user_abort();
up
19
pulstar at mail dot com ΒΆ
22 years ago
These functions are very useful for example if you need to control when a visitor in your website place an order and you need to check if he/she didn't clicked the submit button twice or cancelled the submit just after have clicked the submit button. 
If your visitor click the stop button just after have submitted it, your script may stop in the middle of the process of registering the products and do not finish the list, generating inconsistency in your database.
With the ignore_user_abort() function you can make your script finish everything fine and after you can check with register_shutdown_function() and connection_aborted() if the visitor cancelled the submission or lost his/her connection. If he/she did, you can set the order as not confirmed and when the visitor came back, you can present the old order again.
To prevent a double click of the submit button, you can disable it with javascript or in your script you can set a flag for that order, which will be recorded into the database. Before accept a new submission, the script will check if the same order was not placed before and reject it. This will work fine, as the script have finished the job before.
Note that if you use ob_start("callback_function") in the begin of your script, you can specify a callback function that will act like the shutdown function when our script ends and also will let you to work on the generated page before send it to the visitor.
up
14
Jean Charles MAMMANA ΒΆ
18 years ago
connection_status() return ABORTED state ONLY if the client disconnects gracefully (with STOP button). In this case the browser send the RST TCP packet that notify PHP the connection is closed.
But.... If the connection is stopped by networs troubles (wifi link down by exemple) the script doesn't know that the client is disconnected :(

I've tried to use fopen("php://output") with stream_select() on writting to detect write locks (due to full buffer) but php give me this error : "cannot represent a stream of type Output as a select()able descriptor"

So I don't know how to detect correctly network trouble connection...