How to disable PHP Error reporting in CodeIgniter? How to disable PHP Error reporting in CodeIgniter? codeigniter codeigniter

How to disable PHP Error reporting in CodeIgniter?


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 environmentif ($_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.');    }}


Change CI index.php file to:

if ($_SERVER['SERVER_NAME'] == 'local_server_name') {    define('ENVIRONMENT', 'development');} else {    define('ENVIRONMENT', 'production');}if (defined('ENVIRONMENT')){    switch (ENVIRONMENT){        case 'development':            error_reporting(E_ALL);        break;        case 'testing':        case 'production':            error_reporting(0);        break;        default:            exit('The application environment is not set correctly.');    }}

IF PHP errors are off, but any MySQL errors are still going to show, turn these off in the /config/database.php file. Set the db_debug option to false:

$db['default']['db_debug'] = FALSE; 

Also, you can use active_group as development and production to match the environment https://www.codeigniter.com/user_guide/database/configuration.html

$active_group = 'development';$db['development']['hostname'] = 'localhost';$db['development']['username'] = '---';$db['development']['password'] = '---';$db['development']['database'] = '---';$db['development']['dbdriver'] = 'mysql';$db['development']['dbprefix'] = '';$db['development']['pconnect'] = TRUE;$db['development']['db_debug'] = TRUE;$db['development']['cache_on'] = FALSE;$db['development']['cachedir'] = '';$db['development']['char_set'] = 'utf8';$db['development']['dbcollat'] = 'utf8_general_ci';$db['development']['swap_pre'] = '';$db['development']['autoinit'] = TRUE;$db['development']['stricton'] = FALSE;$db['production']['hostname'] = 'localhost';$db['production']['username'] = '---';$db['production']['password'] = '---';$db['production']['database'] = '---';$db['production']['dbdriver'] = 'mysql';$db['production']['dbprefix'] = '';$db['production']['pconnect'] = TRUE;$db['production']['db_debug'] = FALSE;$db['production']['cache_on'] = FALSE;$db['production']['cachedir'] = '';$db['production']['char_set'] = 'utf8';$db['production']['dbcollat'] = 'utf8_general_ci';$db['production']['swap_pre'] = '';$db['production']['autoinit'] = TRUE;$db['production']['stricton'] = FALSE;


i know i am very late but you can just do this : Find below code in index.php file

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

and replace development with production