CodeIgniter Forums
How to prevent CI from reporting PHP Notice errors - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forumdisplay.php?fid=9)
+--- Thread: How to prevent CI from reporting PHP Notice errors (/showthread.php?tid=73987)



How to prevent CI from reporting PHP Notice errors - shepparddigital - 07-03-2019

I don't have much experience with CI, but I have inherited a project that uses it.

As the title says, how do you prevent CI from reporting PHP notice errors?

In the index.php file there's the following;

PHP Code:
switch (ENVIRONMENT) {
 
   case 'development':
 
       ini_set('display_errors'1);
 
       ini_set('display_startup_errors'1);
 
       error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
 
       break;
 
   case 'testing':
 
   case 'production':
 
       ini_set('display_errors'0);
 
       if (version_compare(PHP_VERSION'5.3''>=')) {
 
           error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
 
       } else {
 
           error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
 
       }
 
       break;
 
   default:
 
       header('HTTP/1.1 503 Service Unavailable.'TRUE503);
 
       echo 'The application environment is not set correctly.';
 
       exit(1); // EXIT_ERROR
}

I have checked that the environment is set to productionso PHP shouldn't be reporting notice errors, but for some reason CI is still displaying them? 



RE: How to prevent CI from reporting PHP Notice errors - php_rocs - 07-03-2019

@shepparddigital,

What version of CI is it?

If it is 3.x then here is the documentation: https://codeigniter.com/user_guide/general/errors.html?highlight=php%20error#error-handling
- in your switch case for 'production' add this statement: error_reporting(0);


RE: How to prevent CI from reporting PHP Notice errors - shepparddigital - 07-04-2019

Yes, it's 3.x, I've followed the documentation, and you can clearly see that on the 'development' environment I've changed the log level via error_reporting() to display all errors except notices, strict errors and user notices, but for one reason or another CI is still outputting those errors.

In the file application/config.php I have also set $config['log_threshold'] = 1;

Why is CI still outputting notices? What am I missing?


RE: How to prevent CI from reporting PHP Notice errors - hc-innov - 07-10-2019

(07-04-2019, 02:06 AM)shepparddigital Wrote: Yes, it's 3.x, I've followed the documentation, and you can clearly see that on the 'development' environment I've changed the log level via error_reporting() to display all errors except notices, strict errors and user notices, but for one reason or another CI is still outputting those errors.

In the file application/config.php I have also set $config['log_threshold'] = 1;

Why is CI still outputting notices? What am I missing?

Very strange!
I tested on my server and it works...
1. perhaps'a you may have an overlaod controller for Exceptions (search under application/core)
2. maybe you have a try-catch-finally block on your own controller
3. be sure the CI code is clean: you must have in line 612 (system/core/Common.php):

PHP Code:
if (($severity error_reporting()) !== $severity)
        {
            return;
        } 

can you post the controller source code that has this problem


RE: How to prevent CI from reporting PHP Notice errors - johnmario - 09-06-2019

if (defined('ENVIRONMENT'))
{
  switch (ENVIRONMENT)
  {
    case 'development':
    case 'testing':
    case 'staging':
      error_reporting(E_ALL & ~E_NOTICE);
    break;

    case 'production':
      error_reporting(0);
    break;

    default:
      exit('The application environment is not set correctly.');
  }
}
See the official PHP Documentation about the error_reporting function for more configuration options.

Alternatively, you can just call the error_reporting function in the particular file you're using (whatever your cron job calls) and it'll overwrite the configuration with what you set in index.php. Here's a simple example using a controller:

class Sample_Controller extends CI_Controller {

  public function __construct()
  {
    parent::__construct();
    error_reporting(E_ALL & ~E_NOTICE);
  }

}

*SEO SPAM REDACTED**


RE: How to prevent CI from reporting PHP Notice errors - InsiteFX - 09-07-2019

You can set the environment in your .htaccess file that resides with index.php

Add to the top of it:

Code:
    # Multiple Environment config, set this to development, testing or production
    # Set the CodeIgniter Environment.
    SetEnv CI_ENV development
    #SetEnv CI_ENV production



RE: How to prevent CI from reporting PHP Notice errors - dave friend - 09-08-2019

SetEnv is an Apache thing, so if your server is NGINX or other http server you will need to find the way your setup works with environmental variables.

If you are using Apache but following @InsiteFX's advice does not work then check that Apache has the env_module is enabled.