tags:

views:

42

answers:

3

hi!

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

A: 

try to add ob_implicit_flush(); in the head

Marcx
thanks but this answer did not resolve my problem. It gives the same result in situation.
A: 

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...

Marcx
A: 

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