Vasteras - Sweden, Belgrade, Nis - Serbia

September 3, 2010

Troxo - Software Development Company

PHP Error Reporting on Production and Development Servers

June 21st, 2007
Tags:, , ,

In case you already run your PHP powered web site, you know how important tweaking of error reporting is for transition between dev and production server. Here’s how we do it at Troxo.

PHP’s error reporting is very useful in discovering the nature of errors in the code that you write. Having errors displayed in the browser while developing an app is a useful way to receive immediate feedback, and this can speed up the development process.

In application production, such behavior is unwanted, embarrassing and worst of all, considered to be a security risk. If an application in production displays errors, vital information about your application is revealed to the public.

PHP configuration settings could be done through making changes in php.ini configuration file. When using PHP as an Apache module, you can also change the configuration settings using directives in Apache configuration files (e.g. httpd.conf) and .htaccess files. To do this you will need "AllowOverride Options" or "AllowOverride All" privileges to do so.

Reference: http://www.php.net/manual/en/configuration.changes.php

If you do not have the privileges to edit php.ini or httpd.conf configuration files the easiest way to configure PHP error reporting is via .htaccess file.

.htaccess file contains one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.

Error reporting on development server

Create .htaccess file and save it in web application root with following contents:

php_value error_reporting 2047
php_flag display_errors true
php_flag display_startup_errors true
php_flag log_errors true
php_value log_errors_max_len 100M
php_flag ignore_repeated_errors false
php_flag ignore_repeated_source false
php_flag report_memleaks true
php_flag track_errors true
php_flag html_errors false
php_value error_log /path/to/error/log

Make sure that /path/to/error/log file exists and is writable by web server user.
With such settings on development server all PHP errors will be displayed and logged at the file at the same time.

For setting value for directive error_reporting you may use these constant names in php.ini but not outside of PHP, like in httpd.conf or .htaccess, where you’d use the bitmask values instead.

Description of configuration directives:
error_reporting – Set the error reporting level. Setting this to E_ALL (2047) enables reporting all types of errors including E_NOTICE which will warn you about possible bugs in your code.

display_errors – Defines whether errors should be printed on output or hidden from user.

display_startup_errors – Even when display_errors is on, errors that occur during PHP’s startup sequence are not displayed. It’s strongly recommended to keep display_startup_errors off, except for debugging.

log_errors – Defines whether PHP errors should be logged to web server log or to PHP error log file which is defined by error_log directive.

log_errors_max_len – Set the maximum length of log_errors in bytes.

ignore_repeated_errors – Do not log repeated messages. Repeated errors must occur in the same file on the same line until ignore_repeated_source is set true.

ignore_repeated_source – Ignore source of message when ignoring repeated messages. When this setting is On you will not log errors with repeated messages from different files or sourcelines.

report_memleaks - If this parameter is set to Off, then memory leaks will not be shown (on stdout or in the log).

track_errors – If enabled, the last error message will always be present in the variable $php_errormsg

html_errors – Turn off HTML tags in error messages.

error_log – Defines name of the file where script errors should be logged. The file should be writable by the web server’s user.

Error reporting on production server via .htaccess file in application root

php_value error_reporting 2047
php_flag display_errors false
php_flag display_startup_errors false
php_flag log_errors true
php_value log_errors_max_len 100M
php_flag ignore_repeated_errors false
php_flag ignore_repeated_source false
php_flag report_memleaks true
php_flag track_errors true
php_flag html_errors false
php_value error_log /path/to/error/log