PHP (Hypertext Preprocessor) is a scripting language where the PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module to generate dynamic web page documents. As a programming language, PHP engine provides comprehensive error logging capability, including output the error details to web pages (display), file or syslog.

Depending on web host and web hosting control panel, PHP may or may not log errors to file by default. On self-installed PHP, the errors are displayed and published to web pages by default, allowing users to see and view the error, if any, via a web browser.

Displaying error messages directly on web pages output is OK, but the error messages tend to have and reveal various important and sensitive information about the web server in it. For example, PHP script name and path, database name and schema, and worse, the security information such as login name and password to access the database or other connection. Thus, on production server, even PHP developer is recommending to turn off and display the print out of errors as a part of the output feature.

For webmasters, bloggers or server administrators who still see the PHP errors been printed and output to web pages, here’s how to log all PHP errors to a log file, with configurable name instead.

  1. Login to the web server, and edit the php.ini file with any editor such as vi.

    Note: php.ini configuration file can be found at /etc/php.ini or /usr/local/lib/php.ini.

  2. Locate display_errors directive, and change its value to Off so that the line looks like following (if not found, add in the line) to disable error output to web pages:

    display_errors = Off

  3. Locate log_errors directive, and change its value to On so that the line looks like following (if not found, add in the line) to enable error logging to file:

    log_errors = On

  4. Locate error_log directive, and modify its value to a preferred file name to save the error log file so that the line looks like following (if not found, add in the line):

    error_log = error_log

    Note: Path can be included, for example, error_log = /var/log/error_log. If no path is specified, the error log file may be stored at the same directory where PHP scripts are located.

  5. Save the modified php.ini.
  6. Restart the web server. For example, Apache web server restarting command is:

    # /etc/init.d/httpd restart

Tip: To log the PHP errors to syslog or Windows NT or Windows Server Event Log, just uncomment or add in the following line:

error_log = syslog

Once started logging to file, the PHP error messages can be viewed at the file name specified in error_log directive, with any text editor such as vi or using tail command to get latest errors. For continuous updated display of new entry to the error log file, use the following command which will update the last 10 lines of error messages and append new lines to the display as new errors are added to the file:

tail -f /path/to/error_log

Related Posts