Nov 6, 2014

f Comment

PHP: Simple Ways To Debug a Production Server without Logging Errors!

Amazon If you run a website with PHP you may find it hard to turn on error logging or error displaying when you are trying to debug a problem on live. Suppose I don't care if visitors see error messages on the live website. I just want to debug. But it turns out I cannot enable displaying errors on my website no matter how I mess with PHP configuration file, or php.ini. What should I do?

I can try to turn on error logging. What if I just don't know how to turn on error logging on my server? Read on to see a couple of simple ways to debug without logging or displaying errors.

Solution

First you should have some idea what the offending line is. In my case, my Javascript was using a function which expects a JSON result from my server, but my server sends a non-JSON-formatted result due to some error.

If the offending line is a function call such as SEmailUtil::emailMeFailure, then adding @ in front of it would suppress any error raised when the function is executed:

@SEmailUtil::emailMeFailure(...);

A usual cause of an error is denied file access due to lack of permission. Consider the following line of code in PHP:

$fh = fopen("/home/ubuntu/log/op/email-sent.log", 'a+') or die("can't open email-sent.log");

This line of code could be the problem. Your web server needs write access to /home/ubuntu/log/op/email-sent.log.

So simply scan the suspicious offending functions and see if they contain lines of code that need file permission for your web server's user. Suppose your web server's user is www-data, then the following command grants the web server access to the file 'email-sent.log':

sudo chown www-data:www-data email-sent.log

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael