关闭 PHP 和 MySQL 上的警告和错误 [英] Turn off warnings and errors on PHP and MySQL
问题描述
我收到了预期的通知和警告,并想在我的 PHP 文件中关闭它们.错误是:
I am getting expected notices and warnings and would like to turn them off in my PHP file. The error is:
Warning: fsockopen()
通知如下:
Notice: A non well formed numeric value encountered in
我打算在这个 PHP 脚本中使用 cron,并且不想在任何地方记录任何错误或通知.
I am planning to use cron for this PHP script and do not want to get any errors or notices logged anywhere.
推荐答案
当您确定您的脚本完美运行时,您可以摆脱这样的警告和通知: 将此行放在您的 PHP 脚本的开头:
When you are sure your script is perfectly working, you can get rid of warning and notices like this: Put this line at the beginning of your PHP script:
error_reporting(E_ERROR);
在此之前,在处理脚本时,我建议您正确调试脚本,使所有通知或警告一一消失.
Before that, when working on your script, I would advise you to properly debug your script so that all notice or warning disappear one by one.
因此您应该首先将其设置为尽可能详细:
So you should first set it as verbose as possible with:
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
更新:如何记录错误而不是显示错误
UPDATE: how to log errors instead of displaying them
正如评论中所建议的,更好的解决方案是将错误记录到文件中,这样只有 PHP 开发人员才能看到错误消息,而不是用户.
As suggested in the comments, the better solution is to log errors into a file so only the PHP developer sees the error messages, not the users.
一个可能的实现是通过 .htaccess 文件,如果您无权访问 php.ini 文件(source).
A possible implementation is via the .htaccess file, useful if you don't have access to the php.ini file (source).
# Suppress PHP errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0
# Enable PHP error logging
php_flag log_errors on
php_value error_log /home/path/public_html/domain/PHP_errors.log
# Prevent access to PHP error log
<Files PHP_errors.log>
Order allow,deny
Deny from all
Satisfy All
</Files>
这篇关于关闭 PHP 和 MySQL 上的警告和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!