关闭PHP和MySQL上的警告和错误 [英] Turn off warnings and errors on PHP and MySQL

查看:162
本文介绍了关闭PHP和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

我计划将cron用于此PHP脚本,并且不想在任何地方记录任何错误或通知.

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文件(

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆