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

31

我已阅读官方文档,他们只说我应该在主index.php文件的顶部拥有error_reporting()函数。 但是我在我的项目中没有index.php文件。我的基本控制器称为core,因此要进入主索引,我需要前往www.mysite.dom/core/。所以我猜这个错误报告功能应该放在这个控制器里面?然后我想知道应该将其放在控制器的哪个位置以及放什么内容以禁用报告。感谢大家的帮助,我想我可能漏了什么 :/

4个回答

83

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

所以你是将 ini_set('display_errors', 0); 添加到 index.php 中吗?我有点困惑,因为你说“使用此代码,您可以在不更改 index.php 的情况下部署您的网站”,但实际上看起来你确实在更改 index.php,我错了吗? - user1227065
1
我只改了一次,它在我的开发机上显示错误,但在生产服务器上却没有显示。 - Andrew Surzhynskyi
@user1227065 如果你想要有几个不同的环境,你需要进行更改。顺便说一下,这些第一行已经在源代码中更改为 define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');(后面不需要 if)。 - CPHPython

6

将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作为developmentproduction来匹配环境。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

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

<?php
  error_reporting(0);
?>

0

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

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

development 替换为 production


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接