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

时间:2013-12-25 14:28:10

标签: php codeigniter

我已经阅读了官方documentation,他们所说的是我应该在主index.php文件的顶部设置一个 error_reporting()函数。但我不知道我的项目中没有index.php个文件。我的基本控制器名为core,因此要转到主索引,我会转到www.mysite.dom/core/。所以我猜这个错误报告功能应该在这个控制器里面?那么我想知道的是我应该把它放在控制器中以及放在其中的内容以禁用报告。谢谢大家的帮助,猜猜我错过了什么:/

4 个答案:

答案 0 :(得分:59)

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

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

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

使用此代码,您可以将站点部署到生产服务器,而无需每次都更改index.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.');
    }
}

答案 1 :(得分:4)

将CI index.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;

答案 2 :(得分:0)

我知道我很晚了,但你可以这样做:在 index.php 文件中找到以下代码

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

并将开发替换为生产

答案 3 :(得分:0)

无需键入长查询即可禁用 codeigniter 中的错误报告。在页面中使用 error_reporting(0); 可禁用在该页面中显示错误。

<?php
  error_reporting(0);
?>