如何在 CodeIgniter 中禁用 PHP 错误报告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20773786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable PHP Error reporting in CodeIgniter?
提问by Andrew Surzhynskyi
I've read the official documentationand all they say is that I should have a error_reporting() function located at the top of your main index.php file.But I don't have index.phpfile in my project. My base controller is called coreso to get to main index I go to www.mysite.dom/core/. So I guess this error reporting function should be inside this controller? Then what I would like to know is where should I put it in the controller and what to put inside of it to disable the reporting. Thank you all for help, guess I am missing something :/
我已经阅读了官方文档,他们只是说我应该在主 index.php 文件的顶部有一个error_reporting() 函数。但是index.php我的项目中没有文件。我的基本控制器被调用,core以便进入我转到的主索引www.mysite.dom/core/。所以我猜这个错误报告功能应该在这个控制器内部?然后我想知道的是我应该把它放在控制器的什么地方以及在它里面放什么来禁用报告。谢谢大家的帮助,我想我错过了什么:/
回答by Andrew Surzhynskyi
Here is the typical structure of new Codeigniter project:
这是新的 Codeigniter 项目的典型结构:
- 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.
我通常在我的 CI index.php 中使用这段代码。只需将 local_server_name 更改为您本地网络服务器的名称。
With this code you can deploy your site to your production server without changing index.php each time.
使用此代码,您可以将站点部署到生产服务器,而无需每次都更改 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.');
}
}
回答by Adrian P.
Change CI index.php file to:
将 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.');
}
}
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:
如果 PHP 错误已关闭,但任何 MySQL 错误仍会显示,请在 /config/database.php 文件中关闭这些错误。将 db_debug 选项设置为 false:
$db['default']['db_debug'] = FALSE;
Also, you can use active_groupas developmentand productionto match the environment https://www.codeigniter.com/user_guide/database/configuration.html
此外,您可以使用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;

