Tracking your website PHP Errors, via the .htaccess file

April 18, 2010

in Install

Tracking PHP ErrorsIt is very important that you monitor your web site for potential PHP errors. If PHP errors are left undetected, your site may not perform well, waste valuable bandwidth, and could be subject to malicious PHP hacking attacks. PHP errors are often triggered by changes in your server configuration, PHP database setup, and files. Furthermore, your site can appear to be operating correctly on the surface. However, your site can be producing PHP errors that are imposing security risks, and they need to be fixed ASAP!

In this blog post, I will show you how to monitor your site for potential PHP errors using the .htaccess file. We will accomplish this by adding some code to the .htaccess file and creating, the following PHP files.
php-errors.log
php-errors-path.php
make-php-errors.php

Please note” this PHP script will only work on Linux based servers!

Getting Started:
First, you need to create the php errors log file. Open up your text editor, create a new file without any content and save it as, “php-errors.log”. Make sure that the “wordwrap” is off, and it doesn’t append the “.txt” file extension to the saved file.

When you are finished, you will have created and saved the following file.
php-errors.log

Next, we will need to add the following code to your .htaccess file.

# Start – Log PHP Errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_flag  log_errors on
php_value error_log /home/path/logs/php-errors.log
# End

Accessing the .htaccess file:
You will need to access the .htaccess file via, Plesk, Cpanel or FTP, and open it with a text editor.
The .htaccess file is usually located in the root directory, unless you have installed the site in a sub-folder.
If you don’t have a .htaccess file, please read my previous post,  how to create a .htaccess file.

You will need to edit the following code with the absolute directory path of your log.
php_value error_log /home/path/logs/php-errors.log”

If you don’t know the absolute path where the php-errors.log file resides, you will need to create the following php script that will display the absolute path.

Open up your text editor and insert the following code in it.
<h3><?php echo $_SERVER['DOCUMENT_ROOT']; ?></h3>
Next save it as “php-errors-path.php”

When you are finished, you will have created and saved the following file.
php-errors-path.php

Make sure that the file doesn’t include the “.txt” file extension. In some cases, you will have to manually rename the file, adding the “‘php” extension.

Creating the sub folder:
Next, you will need to create the sub folder, in this case the code calls for the “log” folder. You can accomplish this via, Cpanel, Plesk or FTP.
Once you have created the log directory, you need to upload the php-errors-path.php, php-errors.log and make-php-errors.php file to the log folder. The next step, your server needs write access to the “log” folder and the php-errors.log file. If you don’t know how to set the appropriate permissions, contact your system administrator for details.

Discovering the absolute path of the “log” sub folder:
Call the php-errors-path.php file from the folder location, where you uploaded it to.
Example: http://www.your-domain.com/log/php-errors-path.php

Creating the error script:
Open up your text editor, and add the following content without the brackets! Save it as, “make-php-errors.php”. Make sure that the “wordwrap” is off, and it doesn’t append the “.txt” file extension to the saved file.
<?php echo "error" ?>"

When you are finished, you will have created and saved the following file.
make-php-errors.php

Testing the PHP test error file, creating an error and message:
Call the make-php-errors.php file from the folder location, where you uploaded it to.
Example: http://www.your-domain.com/log/make-php-errors.php
Results: “error

Remove the following files from your server, after you have tested the script:
php-errors-path.php
make-php-errors.php

Viewing the PHP error log file:
You will need to access the error log file via, Plesk, Cpanel or FTP, and open it with a text editor.

Backing up your files and folders, via FTP, Cpanel or Plesk.
Since the sub folder and php-errors.log needs write permission, “DO NOT” dump them in the root folder. I strongly suggest that you backup your site using Plesk, Cpanel or by FTP before attempting this modification. A word of caution for FTP users. Not all FTP client software programs will detect the .htaccess file. Some FTP programs “FTP Voyager” requires special character list parameters such as “-al”. Make sure that you don’t overwrite your .htaccess file!

Additional thoughts:
I have tested this method for tracking PHP errors and it works very well. I did find in some cases, the write permissions for the file and folder needs to be set to “777″. Therefore, “Don’t dump these files into your Root folder” and create a security risk! When working with the .htaccess file, use extreme caution and back the file up before making any edits to it!

I suggest that you test the “log folder” to see if it is accessible to the public. If you can view the folder contents via your browser, I suggest that you upload a blank .index.html file into the log folder. This will keep people from accessing the log folder contents.

Making the index.html file:
Open your text editor, create new file, save as index.html
If you want meta tags, then create the index.html file with a HTML editor.
Make sure that the file doesn’t include the “.txt” file extension.

When you are finished, you will have created and saved the following file.
index.html

I have created the required files needed for this modification, except for the .htaccess file.  Download –>. Track PHP Errors, via the .htaccess file.

Disclaimer: You agree that by trying/using this modification, that you will not hold me responsible for any damages that may occur to your website.

Hope this blog post was helpful to you, “Tracking your website PHP Errors, via .htaccess file

Spunky Jones.

Comments on this entry are closed.