views:

52

answers:

4

I am running PHP and nginx and I use a production version of php.ini. So the variable display_error is set to Off and there is a good reason I want it this way. But for certain files I need to enable error reporting. I used ini_set to turn on error reporting. But a simple code snippet like:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

echo "hi"errorrrrr
?>

does not trace the error. It simply returns a HTTP 500 Internal Server Error message. What should I do to enable error reporting?

+1  A: 

You should be able to use log_errors to log all of your errors to file. You can even use error_log to write errors to a specific file, if that will make life easier for you.

Justin Ethier
This also doesn't work! Strange. I'm using: ini_set("error_log", "/home/me/log.log");ini_set("log_errors", true);Yes, the log file is writable by the webserver process.
ErJab
Hmmm... Do you have any reason for not just enabling log_errors directly in php.ini? It will not be visible to users but would record any issues across your entire site.
Justin Ethier
Oh yeah, I could do that. Thanks.
ErJab
But then, why does this not work in the first place? Any idea?
ErJab
The reason it doesn't work in the first place is that, since there are syntax errors in your file, PHP does not parse it at all. Thus, it doesn't execute the `log_errors` calls and has to rely on the settings in `php.ini`.
cmptrgeekken
+1  A: 

Of course PHP won't execute these directives if syntax error occurred. Because this script didn't even start to execute! As it failed at parsing phase.

You have to use another way to set these directives, although it can be tricky. Looks like you have your PHP running as CGI, not apache module, so, the only way to set ini directives is to edit php.ini itself.

To be sure, please run phpinfo(); and see what it says of Server API.

Anyway, you have to check apache's error_log every time you see 500 error, to find out what certainly happened.

Col. Shrapnel
+1  A: 

Try setting the ini settings in a .htaccess file.

The settings won't take hold if you put them in a PHP file that doesn't get compiled due to a syntax error in the first place.

Alan
+1  A: 

If you simply can't change the configuration of php, there's a trick you can do. Make a new file which includes the file with the error in it. Before doing the include, set the desired configuration.

Example

error_reporting(E_ALL);
ini_set("display_errors", 1);
include 'some other file with a parse error.php';

Then just execute the wrapper script instead. This works because the statements preceding the include will be evaluated first, before your script finally dies from the parse error.

chris