2

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;
}
Charles
  • 50,943
  • 13
  • 104
  • 142
user3523426
  • 836
  • 3
  • 11
  • 26

1 Answers1

5

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)
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • I am afraid, this is not the case. I use no framework, I catch my errors with my own procedures - set_error_handler and register_shutdown_function. My fscokopen fires register_shutdown_function event and error_get_last() returns: **FATAL: fsockopen(): unable to connect to 192.168.0.154:80 (Connection timed out)** – user3523426 Apr 11 '14 at 13:48
  • yep, I tried it. My handler registration code is: ` set_error_handler("errorHandler"); register_shutdown_function("fatalErrorHandler"); ` – user3523426 Apr 11 '14 at 14:01
  • 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()); } } – user3523426 Apr 11 '14 at 14:01
  • Not, here. Edit the question. Important is that you `return true;` in the error handler – hek2mgl Apr 11 '14 at 14:02
  • function errorHandler($errno, $errstr, $errfile, $errline,$vars,$trace="") { // here some formating and checking file_put_contents( .... ); if(error_reporting() > 0) echo("ERROR: [{$errfile}:{$errline}] {$errstr}"); return true; } – user3523426 Apr 11 '14 at 14:03
  • 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. The problem is the shutdown function, not the error handler. – hek2mgl Apr 11 '14 at 14:06
  • I am aware of this - that's why I have error checking in it. Otherwise I would get FATAL every time when I run a script. In case, fsockopen causes just error (or warning), my standard error handler would be called. But have have just the FATAL, not the error. That's why I assume fsockopen causes FATAL ERROR, not normal one. This is happening on my Linux box (Synology NAS station), not on my Windows PC .... – user3523426 Apr 11 '14 at 14:14
  • You get 'FATAL:` because you are writing `FATAL:` hardcoded in the message. This string is not from PHP. Also this is not related to linux or windows. Read my anwer, especially the first paragraph, again, refine your shutdown function, and you'll see – hek2mgl Apr 11 '14 at 14:16
  • Btw, note that `$error['type']` will be `0` if you suppressed a warning using the `@` operator – hek2mgl Apr 11 '14 at 14:26