开发者

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


this works... I tested to my space, works if user close windows, but if user press esc button don't work, I really don't know why...

<?php
ignore_user_abort (TRUE);

$x=0;
while (1) {
    echo "\n";
    if (connection_status()!=0){
                file_put_contents('test.txt',connection_status());
                die();
         }
}
      file_put_contents('test.txt',connection_status());
?>  

The function connection_status return a int value...


try to add ob_implicit_flush(); in the head


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);
    ?>


Nothing of the above worked for me, so I wrote this: https://bitbucket.org/sivann/coolagent/raw/7d024bfff4a7ef3ce90dff14d4953b3347dfeaa2/server/app/src/Netstat.php which actually detects connection status from OS. Linux only.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜