Open In App

PHP | connection_status() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


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.




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


Output:

Connection is in a normal state

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



Last Updated : 29 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads