PHP Error Handling using ini_set or .htaccess

Tracking your site’s PHP errors is an excellent way to manage and troubleshoot unexpected issues related to plugins and themes. While there is no definitive method for handling errors, there are some "best practices" that should be implemented in all PHP applications.

An "error" is an expected or unexpected event that occurs when your PHP code is running. An "expected" error can be a database query that returns no result or an html form that is missing values for designated required fields. An "unexpected" error is one that assumes a particular application state which, for some as yet unknown reason, does not exist. The most obvious unexpected error is a database that is not running or a missing file that one of your scripts expects to find.

Configuration settings for PHP Error handling and reporting are available in php.ini file, which is located in the PHP installation folder of your system. For reference, settings of any PHP configuration can be changed in various ways – using ini_set(), in WINDOWS registry, in php.ini, in .htaccess or in httpd.conf. PHP_INI_ALL indicates that the related configuration can be changed in any the aforementioned ways.

Using PHP to Manage PHP Error Reporting

Enabling Error Reporting in PHP is quite simple. At the top of your script place the following lines of code. This is by far the easiest method and will work in most hosting environments:


error_reporting(E_ALL);
ini_set('display_errors', true);

Using ini_set we defined the value of a configuration option from the PHP.ini file. There is also a list of all directives you can set to configure your PHP setup on the PHP.net manual.

Using .htaccess to Manage PHP Error Reporting

An alternative method would be a technique whereby PHP errors are suppressed via htaccess. This is accomplished by including the following htaccess directives to your domain’s httpd.conf or to your site’s root (or other target directory) htaccess file:


# hide php errors
php_flag display_startup_errors ON
php_flag display_errors ON
php_flag html_errors ON
php_flag log_errors on
php_flag ignore_repeated_errors off
php_flag ignore_repeated_source off
php_flag report_memleaks on
php_flag track_errors on
php_value docref_root 0
php_value docref_ext 0
php_value error_log /home/path/public_html/domain/PHP_errors.log
php_value error_reporting 999999999
php_value error_reporting -1
php_value log_errors_max_len 0
Order allow,deny
Deny from all
Satisfy All

VICTORY

In this tutorial, I’ve hopefully covered the most common techniques for handling failures within PHP applications.

One thought on “PHP Error Handling using ini_set or .htaccess

Leave a Reply

Your email address will not be published. Required fields are marked *