0

I understand this has been asked a bunch of times and probably appropriately answered so I ask you give me some grace here. I want to log errors to the error log but NOT display them to the client and for some reason nothing I've tried is making any difference. I'm currently using error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT); and my PHP.ini has the following for display_errors:

display_errors = Off

I thought maybe the framework I'm using or some code was overwriting that value (possibly via ini_set()) but if I ask PHP what the value is via:

$display_errors = ini_get('display_errors');
if ( filter_var( $display_errors, FILTER_VALIDATE_BOOLEAN) ){
    echo "display_errors = true\n";
} else {
    echo "display_errors = false\n";
}

I get false (Note: I put the code at the end of the view/template for the framework). If I grep or use VSCode to search the codebase for display_errors I don't see any references that override the default behavior. If it makes any difference the actual value of display_errors as reported by ini_get() is an empty string ''. In case it's not clear this is in the context of Apache2 (Apache/2.4.29) on Ubuntu. What am I missing? Is there a third flag around displaying errors? TIA

Edited:
Framework is CodeIgniter v2.2.6 as reported by system/core/CodeIgniter.php.
PHP 7.2.24-0ubuntu0.18.04.13.
Apache2 (Apache/2.4.29)

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
JSP
  • 547
  • 1
  • 4
  • 18
  • You mention a framework, pleae tell us which one, it could be relevant – RiggsFolly Oct 02 '22 at 13:59
  • Create a quick script called check.php containing `` and then run it in the browser. Look at the value for Loaded Configuration File on the first page, make sure you are amending the correct ``php.ini` file – RiggsFolly Oct 02 '22 at 14:11
  • I think this will answer you question : https://stackoverflow.com/questions/16184881/logs-php-error-but-not-display-it-in-browser – Amandeep Singh Oct 02 '22 at 14:12
  • 1
    @RiggsFolly It reports `/etc/php/7.2/apache2/php.ini` which is what I'm editing. EDIT: Technically the setting there has always been `display_errors = Off` and `error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT`. – JSP Oct 02 '22 at 14:17
  • @AmanSingh My reading of that is I've already done the suggested steps. What are you thinking I'm missing? – JSP Oct 02 '22 at 14:18

1 Answers1

1

I believe I found the answer for whatever other poor soul is still stuck on CodeIgniter v2. In system/core/Common.php there is a _exception_handler() function that has the following:

if (($severity & error_reporting()) == $severity)
{
    $_error->show_php_error($severity, $message, $filepath, $line);
}

The above appears to be what's causing errors to be shown to publicly and while could't find CI v2 code on Github I did see it was removed (or possibly just moved) in V3:

CodeIgniter V3 _exception_handler

Here is the complete (v2.2.6) function (unmodified in any way as far as I know):

// --------------------------------------------------------------------

/**
* Exception Handler
*
* This is the custom exception handler that is declaired at the top
* of Codeigniter.php.  The main reason we use this is to permit
* PHP errors to be logged in our own log files since the user may
* not have access to server logs. Since this function
* effectively intercepts PHP errors, however, we also need
* to display errors based on the current error_reporting level.
* We do that with the use of a PHP error template.
*
* @access   private
* @return   void
*/
if ( ! function_exists('_exception_handler'))
{
    function _exception_handler($severity, $message, $filepath, $line)
    {
         // We don't bother with "strict" notices since they tend to fill up
         // the log file with excess information that isn't normally very helpful.
        if ($severity == E_STRICT)
        {
            return;
        }

        $_error =& load_class('Exceptions', 'core');

        // Should we display the error? We'll get the current error_reporting
        // level and add its bits with the severity bits to find out.
        if (($severity & error_reporting()) == $severity)
        {
            $_error->show_php_error($severity, $message, $filepath, $line);
        }

        // Should we log the error?  No?  We're done...
        if (config_item('log_threshold') == 0)
        {
            return;
        }

        $_error->log_exception($severity, $message, $filepath, $line);
    }
}
JSP
  • 547
  • 1
  • 4
  • 18