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 ***
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:
PHP | SEO | web development | | web security | WebGeek
If you enjoyed this post, make sure you subscribe to the RSS feed!
Related Posts:
Comments
2 Responses to “Suppress PHP Errors”
Leave a Reply
If you have any questions about commenting, please see our Comment Policy.











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.
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.)