Site Check – Web Site Advice

Get healthy advice on how to improve your web site and make money with it
Topics covered: Creation, Optimization, Marketing, Maintenance, Hosting, Tuning


PHP debugging

Author: Joe Savelberg 04.09.2008   Print This Post   Email This Post

Sometimes there are little errors in your PHP scripts and you need to debug them. If you don’t have access to the global PHP error log, then it’ll be difficult to see what goes wrong and where.

However if you’re able to put a .htaccess file on your web host, then you can add some little commands to that file to enable PHP logging and displaying. This allows you to find out what’s happening in your script.

Displaying php errors in the browser window:

Put the following code into your .htaccess file:

php_flag display_errors on

Attention: this command will show all php errors in your browser window and other people will see these errors too, when they browse your site. The php errors display the name and path of the files where the error occured (also included files). You should only enable this feature when you really need it. Make sure to disable it afterwards.

Logging php errors to a file

php_flag log_errors on
php_value error_log php.txt

You should replace php.txt with the correct path to your log file. That file needs to be writable by the owner under which PHP runs. Usually this is nobody, apache or www when you’re running PHP as an Apache module. Make sure the directory that contains the log file has the right permissions.

Here’s the complete .htaccess file

php_flag display_errors on
php_flag log_errors on
php_value error_log php.txt

Another handy way for debugging PHP scripts is to use echo $variable whenever you suspect that there is something going wrong in your script. Just put this command after all important steps in your script and display the value of your important variables.

Happy debugging!

Some more debugging links:

Leave a Reply

You must be logged in to post a comment.