How to Save PHP Error Logs to a File

On this page
  1. Step 1: Locate the .htaccess File
  2. Step 2: Enable PHP Error Logging
  3. What Do These Directives Do?
  4. Step 3: Generate an Error
  5. Step 4: Review the Log File
  6. Important Security Recommendation
  7. Summary

When troubleshooting a website or PHP application, it can be helpful to save PHP errors to a log file instead of displaying them directly on the page. This allows you to review errors later without exposing technical details to visitors.

You can enable PHP error logging by adding a few directives to the website’s .htaccess file.

Step 1: Locate the .htaccess File

Connect to your hosting account using FTP and navigate to your website’s main directory, typically:

public_html

Locate the file:

.htaccess

Note: The filename begins with a dot (.). Some FTP clients hide files that start with a dot by default.

Step 2: Enable PHP Error Logging

Add the following lines to the .htaccess file:

php_value error_reporting 1
php_value log_errors 1
php_value error_log /home/client.mybox.com/<LOGIN>/<DIRECTORY>/PHP_errors.log

Replace:

  • <LOGIN> with your hosting account login
  • <DIRECTORY> with the directory where you want the log file to be created

For example:

php_value error_reporting 1
php_value log_errors 1
php_value error_log /home/client.mybox.com/john/public_html/PHP_errors.log

What Do These Directives Do?

Directive Description
error_reporting Enables PHP error reporting
log_errors Tells PHP to save errors to a file
error_log Specifies the location of the log file

Step 3: Generate an Error

After saving the changes, reproduce the issue on your website.

PHP will automatically write error messages to the specified log file.

Step 4: Review the Log File

Open the file specified in the error_log directive:

PHP_errors.log

You will find entries similar to:

[06-Jun-2026 12:34:56] PHP Fatal error: Uncaught Error: Call to undefined function...

These messages can help identify:

  • PHP syntax errors
  • Fatal errors
  • Warnings
  • Notices
  • Missing files or functions

Important Security Recommendation

For production websites, it is generally recommended to:

  • Log errors to a file
  • Avoid displaying errors directly to visitors

Displaying PHP errors publicly may reveal sensitive information about your application or server configuration.

Summary

To save PHP errors to a file, add the appropriate php_value directives to your website’s .htaccess file and specify the location of the log file. Once enabled, PHP will record errors in the designated file, making it easier to diagnose and resolve application issues without exposing error messages to website visitors.

Didn't find an answer?

Our support team is here for you 24/7 – write to us and we will help you sort it out.