Search code examples
phpcodeigniter

How to disable PHP Error reporting in CodeIgniter?


I've read the official documentation and all they say is that I should have a error_reporting() function located at the top of your main index.php file. But I don't have index.php file in my project. My base controller is called core so to get to main index I go to www.mysite.dom/core/. So I guess this error reporting function should be inside this controller? Then what I would like to know is where should I put it in the controller and what to put inside of it to disable the reporting. Thank you all for help, guess I am missing something :/


Solution

  • Here is the typical structure of new Codeigniter project:

    - application/
    - system/
    - user_guide/
    - index.php <- this is the file you need to change
    

    I usually use this code in my CI index.php. Just change local_server_name to the name of your local webserver.

    With this code you can deploy your site to your production server without changing index.php each time.

    // Domain-based environment
    if ($_SERVER['SERVER_NAME'] == 'local_server_name') {
        define('ENVIRONMENT', 'development');
    } else {
        define('ENVIRONMENT', 'production');
    }
    
    /*
     *---------------------------------------------------------------
     * ERROR REPORTING
     *---------------------------------------------------------------
     *
     * Different environments will require different levels of error reporting.
     * By default development will show errors but testing and live will hide them.
     */
    
    if (defined('ENVIRONMENT')) {
        switch (ENVIRONMENT) {
            case 'development':
                error_reporting(E_ALL);
                break;
            case 'testing':
            case 'production':
                error_reporting(0);
                ini_set('display_errors', 0);  
                break;
            default:
                exit('The application environment is not set correctly.');
        }
    }