Search code examples
phpfsockopen

fsockopen causes FATAL error when host is unreachable - "unable to connect to"


When I call fsockopen to an unreachable IP address, PHP stops execution with FATAL ERROR, as it is intended to do. But it's normal situation for me, that a host is unreachable. Is there a way, how to prevent stopping the php even in case of the FATAL ERROR, if it is the intended behavior?

I don't use any framework, I handle all errors myself. I registers the handlers like this:

set_error_handler("errorHandler");
register_shutdown_function("fatalErrorHandler");

and the handlers are defined like this:

function fatalErrorHandler() 
{
    $error = error_get_last();

    if( $error !== NULL) {
      $errno   = $error["type"];
      $errfile = $error["file"];
      $errline = $error["line"];
      $errstr  = $error["message"];

      errorHandler($errno, "FATAL: " . $errstr, $errfile, $errline,get_defined_vars(),debug_backtrace());
    }
} 

function errorHandler($errno, $errstr, $errfile, $errline,$vars,$trace="")
{

   // some formatting and checking

   file_put_contents(dirname(__FILE__) . "/error/" . gmdate("YmdHis") . str_replace(".","",microtime(true)) . ".err"
  ,"<error_log_date>" . gmdate("YmdHis") . "</error_log_date><error_log_uid>{$uid}</error_log_uid>
   <error_log_str>{$errstr}</error_log_str>
   <error_log_file>{$errfile}</error_log_file>
   <error_log_line>{$errline}</error_log_line>
   <error_log_vars>{$vars}</error_log_vars>
   <error_log_trace>{$trace}</error_log_trace>");


 return true;
}

Solution

  • Final answer after the question was edited

    You are not aware of the fact that shutdown function is called at the end of the script regardless of whether errors happened or not. And even if it was just a warning which did not stop the script immediately error_get_last() will return it. Further you are just putting FATAL:, hardcoded in front of the message. You should handle different types of errors, warnings and notices here.

    The problem is the shutdown function, not fsockopen(). The shutdown function should look like this:

    register_shutdown_function(function() {
        $error = error_get_last();
        if($error && $error['type'] === E_ERROR) {
    
            $errno   = $error["type"];
            $errfile = $error["file"];
            $errline = $error["line"];
            $errstr  = $error["message"];
    
            errorHandler($errno, "FATAL: " . $errstr, $errfile, $errline /* , ... */);
        }   
    });
    

    Original answer

    You told that PHP stops execution with FATAL ERROR.

    This is not true. fsockopen will return false and throw a warning(!) in case of error:

    // sorry example.com ;)
    var_dump(fsockopen("www.example.com", 1000, $errno, $errstr, 3));
    

    PHP Warning: fsockopen(): unable to connect to www.example.com:1000 (Connection timed out) in /home/thorsten/src/checkout-plugin/a.php on line 3 PHP Stack trace: PHP 1. {main}() /home/thorsten/src/checkout-plugin/a.php:0 PHP 2. fsockopen() /home/thorsten/src/checkout-plugin/a.php:3 bool(false)

    If you get a fatal error, this can be caused by a global error handler which has been registered using set_error_handler() and turns warnings into exceptions. Some frameworks are doing so. If this is true, you can use the "silence" operator @ to suppress the warning:

    var_dump(@fsockopen("www.example.com", 1000, $errno, $errstr, 3));
    // bool(false)