Suppress PHP Errors

by Scott Allen

The other day I was checking out some tools on a popular site and got the following PHP error in my browser:

Warning: mysql_connect() [function.mysql-connect]: Too many connections in /****/*****/***/*****.php on line ***

It is important to suppress PHP errors.I blocked out specific directories and line numbers in this post, but the real error message laid it out for all the world to see. PHP developers will be familiar with this and similar errors that PHP kicks out.

The site I visited was relatively high profile and this could be potentially embarrassing, not to mention the problems it could cause. There are many reasons you should suppress PHP errors, except when in development and testing phases.

  • Improve user experience.
    Who wants to come to a site and see ugly errors instead of, or in addition to content?
  • Improve/maintain search engine rankings.
    If for some reason, your site breaks, and the first line of code is an error, or the site content is replaced by errors, that can affect your rankings and you may lose position for some of your keywords. All your hard SEO work will be out the window if you don’t catch it quickly.
  • Improve site security.
    Do you really want the world to see the exact line number and command that broke the code of your site? These errors often reveal private include files that the world would never know about. A savvy and unscrupulous hacker could potentially make use of that information.

How to Suppress PHP Errors — It’s Easy

Many web hosts will have the server configured out of the box to let you use PHP flags in your .htaccess file. To do so, simply add a line near the beginning of your .htaccess:

php_flag display_errors off

If they don’t allow you to do it in .htaccess, they may allow you to use a php.ini file. Add the following line to your php.ini:

display_errors = off

When to Enable PHP Errors

There are definitely times when you want to have PHP errors enabled. These include pre-launch development and troubleshooting sessions. During these times it can be extremely helpful to see the exact line number and error that broke your page. As soon as you’ve launched the site, or are finished troubleshooting, you’ll want to disable the errors again.

There may be a few specific exceptions, but in general this is a good rule to follow when developing sites in PHP.

Tags:
| | | | |

Bookmark or Share with Friends: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • StumbleUpon
  • del.icio.us
  • Sphinn
  • Digg
  • Reddit


If you enjoyed this post, make sure you subscribe to the RSS feed!


Email This to a Friend Email This to a Friend

Print This Post Print This Post


Related Posts:

  • Bad Behavior Behaving Badly
  • .Htaccess Reference
  • WP-SpamFree: A Powerful Anti-Spam Plugin for WordPress!


  • About This Entry