Welcome Guest, Not a member yet? Register   Sign In
How to prevent CI from reporting PHP Notice errors
#1

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? 
Reply
#2

(This post was last modified: 07-03-2019, 10:38 AM by php_rocs.)

@shepparddigital,

What version of CI is it?

If it is 3.x then here is the documentation: https://codeigniter.com/user_guide/gener...r-handling
- in your switch case for 'production' add this statement: error_reporting(0);
Reply
#3

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?
Reply
#4

(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
Reply
#5

(This post was last modified: 09-07-2019, 01:10 AM by ciadmin.)

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**
Reply
#6

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
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB