Orofold Labs

Launching soon

Notes Get in touch

Engineering 4 min read Updated

PHP error handling and logging without leaking details

In short

Show visitors a generic message, write the details to the server log, and let uncaught exceptions reach one handler that stops the script cleanly.

Errors in PHP help the developer, but they cause harm when a visitor sees them. A stack trace can reveal file paths, database names and fragments of queries. Keep those details off the screen and still record them. Exceptions differ from errors, and a last-resort handler can stop the script in a controlled way.

Keep the screen and the log apart

The display_errors setting decides whether errors are printed as part of the output. The PHP manual describes it as a development feature that should not be used on production systems, such as systems connected to the internet. For a public site, switch it off in the production configuration. Changes made at runtime with ini_set() have no effect if the script hits a fatal error, so set the value in the configuration file.

The log_errors setting sends messages to the server’s error log. For public sites the manual is direct: it advises error logging in place of error displaying. The error_log setting names the file for these messages. If it is not set, PHP sends them to the error logger of the web server, or to standard error in the command line.

Catch exceptions only where you can act

The manual describes Throwable as the base interface for anything that can be thrown, including Error and Exception. A single catch block for Throwable therefore handles both kinds. Catch exceptions only where you can act on them: retry a request, show a clear message to the visitor or roll back a transaction. Do not catch an exception only to hide it. If the code cannot recover, let the exception reach the handler, where it can be logged once, with its context.

Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea

A last-resort handler

set_exception_handler registers a function for exceptions that no try block caught. According to the manual, execution stops after the callback has run, so the handler is the right place for one final log entry and a generic response page. Keep the handler short, and wrap its own work in a try block, so that a failure inside it cannot leave the visitor with a second, unhandled error.

Write context to the log, not secrets

The error_log function sends a message to the system logger, to a file or to an email address, depending on its message type. The default type sends it to the system logger. The manual notes that with type 0 the function returns true whatever happens, so the return value does not prove that the message was written. Check the destination in a test.

Write the exception class, the message, the file and the line, plus an identifier for the request, so that someone can match a visitor’s report to the entry. Do not write passwords, tokens, card numbers or full personal records. The guidance in logging: what to record and how long covers what belongs in a log and how long to keep it.

A practical checklist

Before you put a site live, work through these steps:

  1. Set display_errors to off in the production configuration, and confirm it with a deliberately broken test page on a copy of the site.
  2. Turn on log_errors, and point error_log at a file outside the public folder that only the web server user can write to.
  3. Throw exceptions for failures, catch them only where you can act, and let the rest reach the handler.
  4. Register set_exception_handler with a short generic response and a single log entry.
  5. Trigger a real exception and check that the visitor sees no path, query or trace.

For the wording that people read, see Error messages that help people fix the problem. For JSON APIs, the response format is a separate question, covered in API error responses.

The statements about display, logging and exceptions come from the PHP manual. The advice on what to leave out of logs and how to test the setup is general practice.

Sources and limits

Common questions

Should I turn off display_errors on a live site?
Yes. The PHP manual says display_errors should never be used on production systems connected to the internet. Show a generic message and keep the details in the log.
Is error_log the same as the web server log?
Not always. If error_log is not set, PHP sends errors to the error logger of the web server or to standard error on the command line. Set the path explicitly.
Will a last-resort exception handler let the script continue?
No. The PHP manual says execution stops after the callback is called, so the handler should log the problem and send one final response.

Sources

  1. Error Handling and Logging Configuration, PHP Manual (php.net). Accessed 24 November 2026
  2. Throwable, PHP Manual (php.net). Accessed 24 November 2026
  3. set_exception_handler, PHP Manual (php.net). Accessed 24 November 2026
  4. error_log, PHP Manual (php.net). Accessed 24 November 2026

Further reading

Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea

All notes