Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I expose PHP errors 1

Status
Not open for further replies.

rrsub

MIS
Oct 23, 2002
536
US
This isn't as easy as it sounds to be.
I'd like to have error reporting temporarily report to the screen.

I have a server, typical LAMP server, that doesn't have PHP errors turned on. Instead it logs them to the error_log for the site, not the server, the site.

So in my PHP.INI, I have:

error_reporting = E_ALL & ~E_NOTICE
display_errors = Off
log_errors = On


If there is an error, I get a blank screen then I have to A) visually recheck my code, or B) check the logs

I would like to get screen reporting on the errors.

None of these work in the PHP script.
ini_set('error_reporting',E_ALL);
ini_set('display_errors','On');
error_reporting(1);

Only if I change
display_errors = Off

to
display_errors = On

then
apachectl -k restart

do I get error reporting on the screen which I really don't want as it could expose the rest of the sites on the server.

Then when I'm finished, I turn off error reporting.

Is there a way to temporarily do this?
My guess is that there is a hidden conf file that I can't find that is preventing this.

 
the ini_set should work but you need to set it to "1" rather than "on". i.e a string of the numeral one;
Code:
ini_set ("display_errors","1");
error_reporting(E_ALL);
 
i can't think why it does not work.

to debug: could you try this and report back?
Code:
ini_set ("display_errors","1");
error_reporting (E_ALL);

echo "Current value of display errors is ". ini_get("display_errors");
echo "<br/>";
echo "Current error reporting level is ". error_reporting();
echo "<br/>";

echo "disabled functions: " . get_cfg_var("disable_functions");
echo "<br/>";

if all is well you should get
Code:
Current value of display errors is 1
Current error reporting level is 2047
disabled functions:

if that is the case then try to generate an error by appending this code to the above:
Code:
echo "This should generate an E_NOTICE<br/>" ;
echo $array[somevalue]; //this is deliberately bogus

you should get an undefined constant and an undefined variable notice

please post back the results
 
one other question. are you sure you are not using a custom error handler? look for a set_error_handler() function in your code.
 
I get
Code:
Current value of display errors is 1
Current error reporting level is 2047
disabled functions:

So with the
Code:
ini_set ("display_errors","1");
error_reporting (E_ALL);

echo "This should generate an E_NOTICE<br/>" ;
echo $array[somevalue]; //this is deliberately bogus


I get the NOTICE report
Code:
Notice: Use of undefined constant somevalue...
Notice: Undefined variable: array in...


I don't get ERROR reports

If I have (another deliberate error)
Code:
print<<<HTML

Blah blah blah
HTML; //There is a space after the semicolon

When I should get the Parse ERROR
Code:
Parse error: parse error, unexpected $ in....

I notice that there is this in the CONF file for the site
Code:
        <IfModule sapi_apache2.c>
                php_admin_flag engine on
                php_admin_value open_basedir "{PATH}:/tmp"
        </IfModule>

And there isn't a custom error handler.

 
i understand now.

now ... this may be a penetrating glance into the glaringly obvious. and if so my apologies.

in this script:

Code:
error_reporting(E_ALL);
ini_set ("display_errors", "1");
print<<<HTML

Blah blah blah
HTML; //There is a space after the semicolon

what is happening is that php parses the whole script for consistency before it executes it. as it fails the parse test then the script does not run and the error_reporting etc calls do not get trapped.

so if you tried
Code:
<?
ini_set("display_errors", "1");
error_reporting(E_ALL);
eval ('
echo <<<STR
some text
STR; ');

?>
it would work as expected.

but ... i suspect knowing why there is a problem doesn't help you much ! and sfaik there is no work around for this.

for what it is worth i found the following quote from Zeev Suraski (of Zend, and responsible author of the php code base - i believe):
We'll have to address this in the documentation. Fatal errors cannot be
trapped in userland because the engine may not be in a stable state - and
we should bail out as quickly and as cleanly as possible.

We might want to create a new class of errors, errors which are fatal
unless they're trapped. I think that a call to an undefined function can
belong to such a class.

Zeev

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top