PHP | connection_status() Function

The connection_status() function is an inbuilt function in PHP which returns the current connection status.

Syntax:

int connection_status( void )

Parameters: This function doesn’t accept any parameters.



Return Value: This function returns the connection status bitfield. The possible values of returned connection status bitfield are:

  • 0:CONNECTION_NORMAL – running normally
  • 1:CONNECTION_ABORTED – aborted by user or network error
  • 2:CONNECTION_TIMEOUT – timed out
  • 3:CONNECTION_ABORTED & CONNECTION_TIMEOUT – aborted and timed out

Note: This function is available for PHP 4.0.0 and newer version.

Below programs illustrate the connection_status() function in PHP.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
switch (connection_status()) {
    case CONNECTION_ABORTED:
        echo'Connection aborted';
        break;
    case CONNECTION_TIMEOUT:
        echo'Connection timed out';
        break;
    case CONNECTION_NORMAL:
        echo'Connection is in a normal state';
        break;
  
    case (CONNECTION_ABORTED & CONNECTION_TIMEOUT):
        echo'Connection aborted and timed out';
        break;
    default:
        echo'Unknown';
        break;
}
?>

chevron_right


Output:

Connection is in a normal state

Program 2: Some output to be sent to browser for connection_status() to work in case of browser breaks or closed.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// This will work even if browser breaks or closed
// Sending this to client's browser
switch (connection_status()) {
    case CONNECTION_ABORTED:
        echo'Connection aborted';
        break;
    case CONNECTION_TIMEOUT:
        echo'Connection timed out';
        break;
    case CONNECTION_NORMAL:
        echo'Connection is in a normal state';
        break;
  
    case (CONNECTION_ABORTED & CONNECTION_TIMEOUT):
        echo'Connection aborted and timed out';
        break;
    default:
        echo'Unknown';
        break;
}
?>

chevron_right


Output:

Connection is in a normal state

Reference: https://www.php.net/manual/en/function.connection-status.php



My Personal Notes arrow_drop_up

I am unrecognized crazy being with random thoughts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.