Oct 3 2007

Temporarily enable PHP error display with an htaccess file

PHP’s error reporting can be extremely helpful for debugging scripts while developing a new application. The problem with letting PHP display those error messages is it has the serious potential to reveal sensitive information about your server environment. For that reason, PHP’s error display generally is (and SHOULD BE) disabled for production sites. However, that can make finding and fixing problems in live code much more difficult.

One solution is to temporarily turn on error display with an htaccess file. Placing a php_flag command in an htaccess file allows for overriding the global settings present in PHP’s INI with a local value. Simply create a plain text file, paste the line of code below into the text file, then name the text file “.htaccess”

php_flag display_errors on

Upload your new htaccess file to the directory containing your broken PHP script and it will turn on PHP’s error reporting. Once you have the error message (hopefully including a line number where the error is occurring), just quickly remove the htaccess file and you’re back to normal with error reporting off.

You can override many of the php.ini directives with an htaccess file. Check out the PHP documentation on changing configuration settings for more information.