【问题标题】:How to disable PHP Error reporting in CodeIgniter?如何在 CodeIgniter 中禁用 PHP 错误报告?
【发布时间】:2014-01-13 10:58:36
【问题描述】:

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

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 所以您要将此ini_set('display_errors', 0); 添加到index.php 中?我有点困惑,因为您说使用此代码,您可以在不更改 index.php 的情况下将站点部署到服务器,但看起来您正在更改 index.php 我错了吗?跨度>
    • 我只更改了一次,它在我的开发机器上显示错误,并且不显示在生产服务器上。
    • @user1227065 如果你想拥有几个不同的环境,无论如何你都需要改变它。顺便说一句,这些第一行已经在源代码中更改为define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');(以后不需要if)。
    【解决方案2】:

    将 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 作为 development 和 production 来匹配环境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;
    

    【讨论】:

      【解决方案3】:

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

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

      并将 development 替换为 production

      【讨论】:

        【解决方案4】:

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

        <?php
          error_reporting(0);
        ?>
        

        【讨论】:

          猜你喜欢
          • 2013-05-31
          • 2016-06-09
          • 2013-07-12
          • 1970-01-01
          • 2014-01-23
          • 2016-05-21
          • 2013-10-05
          • 2011-11-14
          • 2020-11-17
          相关资源
          最近更新 更多