Recipe 20.6. Hiding Error Messages from Users


20.6.1. Problem

You don't want PHP error messages visible to users.

20.6.2. Solution

Set the following values in your php.ini or web server configuration file:

display_errors =off log_errors     =on

You may also set these values using ini_set( ) if you do have access to edit your server's php.ini file.

ini_set('display_errors', 'off'); ini_set('log_errors', 'on');

These settings tell PHP not to display errors as HTML to the browser but to put them in the server's error log.

20.6.3. Discussion

When log_errors is set to on, error messages are written to the server's error log. If you want PHP errors to be written to a separate file, set the error_log configuration directive with the name of that file:

error_log   = /var/log/php.error.log

or:

ini_set('error_log', '/var/log/php.error.log');

If error_log is set to syslog, PHP error messages are sent to the system logger using syslog(3) on Unix and to the Event Log on Windows .

There are lots of error messages you want to show your users, such as telling them they've filled in a form incorrectly, but you should shield your users from internal errors that may reflect a problem with your code. There are two reasons for this. First, these errors appear unprofessional (to expert users) and confusing (to novice users). If something goes wrong when saving form input to a database, check the return code from the database query and display a message to your users apologizing and asking them to come back later. Showing them a cryptic error message straight from PHP doesn't inspire confidence in your web site.

Second, displaying these errors to users is a security risk. Depending on your database and the type of error, the error message may contain information about how to log in to your database or server and how it is structured. Malicious users can use this information to mount an attack on your web site.

For example, if your database server is down, and you attempt to connect to it with mysql_connect( ), PHP generates the following warning:

<br> <b>Warning</b>:  Can't connect to MySQL server on 'db.example.com' (111) in <b>/www/docroot/example.php</b> on line <b>3</b><br>

If this warning message is sent to a user's browser, he learns that your database server is called db.example.com and can focus his cracking efforts on it.

20.6.4. See Also

Recipe 8.9 for how to log errors; Recipe 20.5 for more about setting configuration values with ini_set( ); documentation on PHP configuration directives at http://www.php.net/configuration.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net