如何在CodeIgniter中禁用PHP错误报告?

2022-08-30 15:04:40

我已经阅读了官方文档,他们说我应该有一个error_reporting()函数位于主索引.php文件的顶部。但是我的项目中没有文件。我的基本控制器被调用,所以为了得到我去的主索引。所以我想这个错误报告功能应该在这个控制器里面吗?然后我想知道的是,我应该把它放在控制器中的什么位置,以及把它放在控制器里面什么来禁用报告。谢谢大家的帮助,猜猜我错过了一些东西:/index.phpcorewww.mysite.dom/core/


答案 1

以下是新Codeigniter项目的典型结构:

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

我通常在我的CI索引中使用此代码.php。只需local_server_name更改为本地 Web 服务器的名称即可。

使用此代码,您可以将站点部署到生产服务器,而无需每次都更改索引.php。

// 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.');
    }
}

答案 2

将 CI 索引.php文件更改为:

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.');
    }
}

如果 PHP 错误已关闭,但任何 MySQL 错误仍将显示,请在 /config/database.php 文件中关闭这些错误。将db_debug选项设置为 false:

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

此外,您还可以使用active_group作为开发和生产来匹配环境 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;

推荐