PHP error handler resetting connection.

  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

I'm getting a connection reset whenever I attempt to log errors to a file within my error handler. If I comment out the logging, it works?

PHP Code: [ Select ]
<?php
function depath_err($errno,$errstr,$errpath,$errline)
{
   $errortype = array(
               E_WARNING        => 'E_WARNING',
               E_NOTICE          => 'E_NOTICE',
               E_USER_ERROR      => 'E_USER_ERROR',
               E_USER_WARNING    => 'E_USER_WARNING',
               E_USER_NOTICE    => 'E_USER_NOTICE',
            E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
            E_DEPRECATED => 'E_DEPRECATED',
            E_USER_DEPRECATED => 'E_USER_DEPRECATED'
            );
 
 switch($errno)
 {
 case E_WARNING:
 case E_NOTICE:
 case E_USER_ERROR:
 case E_USER_WARNING:
 case E_USER_NOTICE:
 case E_RECOVERABLE_ERROR:
 case E_DEPRECATED:
 case E_USER_DEPRECATED:
 //Do not show these errors, just log them.
 
    $lf = fopen('C:/Apache2/logs/php_errors.log','a');//Open end of log file for writing
   if($lf !== FALSE)
   {
   fwrite($lf,'['.date('d-M-Y H-i-s').'] '.$errortype[$errno].': '.$errstr.' in '.$errpath.' on line '.$errline."\n");
   fclose($lf);
   }
 return true; //THIS CAUSES PHP NOT TO EXECUTE THE INTERNAL ERROR HANDLER.
 break;
 }
}
set_error_handler('depath_err');
?>
  1. <?php
  2. function depath_err($errno,$errstr,$errpath,$errline)
  3. {
  4.    $errortype = array(
  5.                E_WARNING        => 'E_WARNING',
  6.                E_NOTICE          => 'E_NOTICE',
  7.                E_USER_ERROR      => 'E_USER_ERROR',
  8.                E_USER_WARNING    => 'E_USER_WARNING',
  9.                E_USER_NOTICE    => 'E_USER_NOTICE',
  10.             E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
  11.             E_DEPRECATED => 'E_DEPRECATED',
  12.             E_USER_DEPRECATED => 'E_USER_DEPRECATED'
  13.             );
  14.  
  15.  switch($errno)
  16.  {
  17.  case E_WARNING:
  18.  case E_NOTICE:
  19.  case E_USER_ERROR:
  20.  case E_USER_WARNING:
  21.  case E_USER_NOTICE:
  22.  case E_RECOVERABLE_ERROR:
  23.  case E_DEPRECATED:
  24.  case E_USER_DEPRECATED:
  25.  //Do not show these errors, just log them.
  26.  
  27.     $lf = fopen('C:/Apache2/logs/php_errors.log','a');//Open end of log file for writing
  28.    if($lf !== FALSE)
  29.    {
  30.    fwrite($lf,'['.date('d-M-Y H-i-s').'] '.$errortype[$errno].': '.$errstr.' in '.$errpath.' on line '.$errline."\n");
  31.    fclose($lf);
  32.    }
  33.  return true; //THIS CAUSES PHP NOT TO EXECUTE THE INTERNAL ERROR HANDLER.
  34.  break;
  35.  }
  36. }
  37. set_error_handler('depath_err');
  38. ?>


Is it a syntax error I missed or am I not supposed to do that or what? I'm using PHP 5.30. I also tried using error_log(), funny part is if the path I specify is backslashes (WINDOWS) it doesn't error, but it doesn't log either. If I use plain old forward slashes, it resets the connection and still doesn't log.

I'm well aware of the ability of PHP to log errors on it's own from the ini configuration, however I'm using an error handler for a specific reason.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Just out of curiosity, is there any chance of recursion with your code ?
Maybe due to the error handler handling its' own error ?

I was just getting my connection killed and I traced it back to a recursive function that had a glob call in it. Since glob was returning the original path when there was nothing to find, the recursion was going in an infinite loop and PHP was bailing.
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

Thanks for the tip, I'm looking into it still. Totally escapes me.
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

Oh man, this following line had some sort of error in the string...

PHP Code: [ Select ]
<?php
//...
fwrite($lf,'['.date('d-M-Y H-i-s').'] '.$errortype[$errno].': '.$errstr.' in '.$errpath.' on line '.$errline."\n");
//...
?>
  1. <?php
  2. //...
  3. fwrite($lf,'['.date('d-M-Y H-i-s').'] '.$errortype[$errno].': '.$errstr.' in '.$errpath.' on line '.$errline."\n");
  4. //...
  5. ?>


I figured it out, because I suppressed it with the @ operator and it worked, which I was surprised about, because I thought the error operator was still supposed to pass errors to the handler even in the event of an error, which would be the recursion problem I was looking for.

Now get this, I removed the date() function...

PHP Code: [ Select ]
<?php
//...
fwrite($lf,'[] '.$errortype[$errno].': '.$errstr.' in '.$errpath.' on line '.$errline."\n");
//...
?>
  1. <?php
  2. //...
  3. fwrite($lf,'[] '.$errortype[$errno].': '.$errstr.' in '.$errpath.' on line '.$errline."\n");
  4. //...
  5. ?>


Page loads fine, I did one of these: :scratchhead:

I was like wth could be wrong with the date function, so after checking the manual determining I put everything correctly into it, I checked my ini file:

Code: [ Select ]
;date.timezone = America/New_York


Was commented out... I un-commented it, rebooted, fixed...

The screwed up part is I have a million other date implementations on the website, so how come those didn't error UNLESS they were in the error handler?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

My only guess is that the error handler runs in some safe sort of scope/context where some calculated replacements for missing ini values aren't available.

I really don't know, and I just deleted the copy of the PHP source code I usually look through a couple of days ago while cleaning up and realizing I hadn't looked at it in months.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 45 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.