Emre Emre - 4 months ago 22
PHP Question

Php connection_status

I write a script for a alerter service. I used php connection_status function to get connection status. Because the script require a inf loop. I look some example and try other scripts but connection_status function always return 0. I think i tried all the script ways. Please help me.

<?php
ignore_user_abort (TRUE);

$x=0;
while ($x++ < 20) {
print $x;
sleep (1);
}

switch (connection_status ()) {
case CONNECTION_NORMAL:
$status = 'Normal';
break;
case CONNECTION_ABORTED:
$status = 'User Abort';
break;
case CONNECTION_TIMEOUT:
$status = 'Max Execution Time exceeded';
break;
case (CONNECTION_ABORTED & CONNECTION_TIMEOUT):
$status = 'Aborted and Timed Out';
break;
default:
$status = 'Unknown';
break;
}

file_put_contents('test.txt',$status);
?>


My system;

php-5.3.1-1 and apache-2.2.14-1

Answer

Thanks for all answers! I find a way to solve problem. May be output buffer is the reason for this problem. I run flush(),ob_flush() functions after output and solve the problem! Again thanks alot.

<?php
    ignore_user_abort (TRUE);

    $x=0;
    while ($x++ < 10) {
      print " ";
      flush();
      ob_flush();
      sleep (1);
    }

    switch (connection_status ()) {
    case CONNECTION_NORMAL:
       $status = 'Normal';
       break;
    case CONNECTION_ABORTED:
       $status = 'User Abort';
       break;
    case CONNECTION_TIMEOUT:
       $status = 'Max Execution Time exceeded';
       break;
    case (CONNECTION_ABORTED & CONNECTION_TIMEOUT):
       $status = 'Aborted and Timed Out';
       break;
    default:
       $status = 'Unknown';
       break;
    }

    file_put_contents('testa.txt',$status);
    ?>