PHP Error Logging via .htaccess


Here you will be demonstrated on how to enable the advanced PHP error logging functionality for a domain in a Linux Dedicated Server.






PHP Error while Logging

The web servers access and the error logs do not always provide a sufficient information in determining the source of a PHP error. However, the PHP provides an excellent error reporting and handling options you just need to enable them for taking the advantage.

For logging into the PHP errors for “exapmle.com” website one needs to add the following lines to the .htaccess file in the “web root” folder:

# suppress PHP errors from displaying in the browser
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off

# log PHP errors to a file
php_flag log_errors on
php_value error_reporting 32767
php_value error_log "/path/to/file"

 

  • In the code shown above, the integer 32767 is being used to denote the "E_ALL" level of reporting in the legacy PHP versions. Now depending on the specific versions of PHP, you will be running on that domain name, the integer values might correspond differently to the error verbosity levels. 32767, should be able to continue to function as "E_ALL" in the latest function of PHP.
 
  • Now you need to visit the http://www.php.net/manual/en/errorfunc.constants.php for information on the different levels of error reporting available and the corresponding integer level codes.

 

  • It is best in keeping the log in a directory which is NOT web-accessible; some of the PHP errors might expose the vulnerabilities or the sensitive information which could be useful to the visitor having a malicious intent.
  • Now depending upon the level of verbosity you have selected for the PHP error logging and the frequency of errors, the PHP error logging file could grow at a very fast pace. But one must keep an eye on the size of the file and to have the error logging enabled if you have actively debugged the potential website coding issues.


You will be able to access the log files whenever it is configured to be written. Here the files can be accessed at /path/to/file. Here each error is reported on a new line and each of these lines will be timestamped.

 

[06-Nov-2016 14:50:57] PHP Fatal error: Call to undefined function eccho() in /path/to/example.com/html/samplefile.php on line 2

From the error sample which is mentioned below, the problem is that the "eccho" function is not existing. As we are aware that the “echo” is a type of function in PHP, it can be a safe bet that there was just a typo error in the PHP code on line 2 of the "samplefile.php" file.