Home >Database >Mysql Tutorial >How to Suppress and Log PHP and MySQL Errors and Notices?

How to Suppress and Log PHP and MySQL Errors and Notices?

DDD
DDDOriginal
2024-11-12 03:57:02755browse

How to Suppress and Log PHP and MySQL Errors and Notices?

Suppressing Errors and Notices in PHP and MySQL

While developing PHP scripts, you may encounter various warnings and notices that can be overwhelming. These messages can distract from the core functionality of the script and make it difficult to debug. To mitigate this, you can opt to turn off these warnings and notices.

One such error is:

Warning: fsockopen()

Additionally, you may see the following notice:

Notice: A non well formed numeric value encountered in

If you plan to execute this PHP script using cron, you may prefer to disable these messages to prevent them from being logged.

Disabling Warnings and Notices

To turn off warnings and notices, you can insert the following line at the beginning of your PHP script:

error_reporting(E_ERROR);

This setting suppresses all warning and notice messages, leaving you with only error messages.

Logging Errors Instead of Displaying Them

As an alternative to disabling errors and notices, it is recommended to log these messages into a file. This way, only the developer can access these error messages, preventing them from being displayed to users.

To implement this solution using a .htaccess file:

  1. 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
  1. Enable PHP error logging:
php_flag  log_errors on
php_value error_log  /home/path/public_html/domain/PHP_errors.log
  1. Prevent access to PHP error log:
<Files PHP_errors.log>
 Order allow,deny
 Deny from all
 Satisfy All
</Files>

By implementing this solution, you can ensure that errors are recorded and accessible only to authorized individuals.

The above is the detailed content of How to Suppress and Log PHP and MySQL Errors and Notices?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn