Suppress PHP Errors

by Scott Allen - October 30, 2007 
Filed Under PHP, SEO, Web Design / Development, Website Security

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.

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, Share and Enjoy:
  • Twitter
  • Sphinn
  • StumbleUpon
  • Facebook
  • del.icio.us
  • LinkedIn
  • Google Bookmarks
  • E-mail this story to a friend!
  • Print this article!


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

Related Posts:

  1. Improve Site Security and SEO with One Line of Code
  2. PHP-Based User Authentication



Comments

2 Responses to “Suppress PHP Errors”

  1. Carter on November 2nd, 2007 3:21 pm

    I wasn’t aware those errors could be suppressed. I’d think unless you are troubleshooting, using that little .htaccess hack would be a great move.

  2. Dave on November 8th, 2007 10:20 am

    Adding xdebug in to your PHP mix (in your dev environment of course, not your live environment) can also help. Not only does it give you more information about the error but it also makes the messages enormous and either red-and-blue or -orange-and-red.

    Even the tiniest little error during your development phase will produce an enormous, page-hogging error message which fosters error free coding. (Note, error-free coding is not the same as bug-free coding. Errors are noticed and reported… bugs may not be.)

Leave a Reply
If you have any questions about commenting, please see our Comment Policy.