Zend Framework - not all errors are shown - php

In an action method, I have the following excerpt of code:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Logger::log('test');
The Logger class is defined in this way:
class Logger {
public static function log() {
echo "test";
}
I deliberately forgot the closing brace of the function to demonstrate the problem. When the action is called, absolutely nothing is rendered on the screen. What type of error is this, and why is it not displayed, even though I configured PHP to show all errors, as shown above?
Of course, if I add the missing brace, everything is OK.

You also have to enable display_startup_errors to show Fatal errors:
Even when display_errors is on, errors that occur during PHP's startup sequence are not displayed. It's strongly recommended to keep display_startup_errors off, except for debugging.
Also see the Note for display_errors:
Although display_errors may be set at runtime (with ini_set()), it won't have any affect if the script has fatal errors. This is because the desired runtime action does not get executed.
You can set both values in Zend Framework's application.ini. On a sidenote: if you set error_reporting(-1) it will report (!display) all errors, including E_STRICT and any future additions.

I have exactly the same problem - I didn't manage to completely solve it, but I found out that all errors are correctly logged to a file, even though they are not displayed.
Just put this lines in your .htaccess/server config:
php_value log_errors On
php_value error_log "/path_to_logs/errors.log"

Related

PHP error_reporting issue, even local = On

in my php.ini the display_errors setting is set to Off by standard. Usually I started my index.php file with
ini_set('display_errors', 'On');
error_reporting(E_ALL & ~E_NOTICE);
which all worked fine for my needs.
Recently, the error reporting did not work anymore. notices have been thrown out though, so:
<?php echo $i ?>
throws a notice "undefined constant"
<?php badbadbad ?>
simply returns a blank page instead of a Fatal Error message.
Checking phpinfo() shows that the Master value for display_errors is Off, while the local value is set to On (as expected). Nevertheless I could not get any error message.
Changing my php.ini setting for display_errors = On gets me around this issue, but should not be a solution for ever.
Any hints?
Thanks & brgds
David
I think the reason your second example fails is that it's actually a Parse Error.
When executing code, PHP has to first parse and compile the whole file it's going to execute and then only after that it can run it. If there's a problem during parsing, even if the problem is at the end of the file, none of the file's code will be executed -- so neither will your ini_set() and error_reporting() statements.
If the erroneous code is in the same file as said statements, there's not much you can do. If it's in another file that is used with include or require, make sure that the including calls are made after setting the new error reporting values. At least that works for me with PHP >= 5.4.

Not getting PHP errors?

For some reason on this particular script, which is a copy of a script I use in a lot of other places, I am not getting any PHP errors. It simply shows a blank page, and It took me a long long time to hunt down a missing semi-colon this morning. Why arn't errors showing up?
my PHP.INI for this sub-domain:
display_errors = On
short_open_tag = On
memory_limit = 32M
date.timezone = Europe/Paris
The code at the top of the page:
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
The Sub Domain is set to run PHP as an Apache Module Which is the same as every other domain I use.
So I am not sure why I am not getting errors displayed. Can anyone tell me?
EDIT:
This is solved, because the errors I was producing were on the page where I had got the lines:
error_reporting(E_ALL);
ini_set('display_errors', '1');
written. When I put the error on to a seperate page and included it, I could see the error fine.
I guess that's why they use bootstrapping!
You should set error_reporting to E_ALL in the php.ini as well: when a parse error occurs (such as a missing semicolon), your error_reporting(E_ALL) won't be used.
You could try to change this lines to get more information from php:
; The display of errors which occur during PHP's startup sequence are handled
; separately from display_errors. PHP's default behavior is to suppress those
; errors from clients. Turning the display of startup errors on can be useful in
; debugging configuration problems. But, it's strongly recommended that you
; leave this setting off on production servers.
display_startup_errors = On
; When PHP displays or logs an error, it has the capability of formatting the
; error message as HTML for easier reading. This directive controls whether
; the error message is formatted as HTML or not.
; Note: This directive is hardcoded to Off for the CLI SAPI
html_errors = On
error_reporting(E_ALL);
ini_set('display_errors',1);
ini_set('display_startup_errors',1);

Logging PHP notice errors

I have recently taken over development of a legacy system and want to be able to turn on logging of PHP E_NOTICE's on the deployment environment. The deployment environment ini has the following directives...
error_reporting = E_ALL & ~E_DEPRECATED
display_errors = Off
log_errors = On
I have compared the error_reporting bitmask by using echo (E_ALL & ~E_DEPRECATED).' = '.error_reporting();, and both match, so I know the error_reporting level isn't changed within the system itself, and if I turn display_errors = On notices are displayed, but not logged.
So how can I start logging PHP E_NOTICE's?
Update:
According to #m.p.c in the comments on this answer, errors are being displayed in the browser when display_errors is on, but they aren't appearing in the log. I was assuming errors weren't appearing at all.
Are E_NOTICE errors the only ones that aren't appearing in the log, or are all error types affected? If it's all error types, then the first thing I would check is whether or not error logging is even enabled. Try setting ini_set('log_errors', 'On'); at the top of your script. If that doesn't work, then try setting your log file to something you're sure your server can write to by calling ini_set('error_log', 'your_file_path');. If neither of these work, then I think something is seriously wrong with your PHP install. If either of these fixes work, you can put them into your actual php.ini if you have access.
Original Answer:
Based on the error_reporting level in your question, your PHP install should already be setup to report E_NOTICE errors. If it's not logging these errors, something is wrong. I would suggest turning on display_errors to see if any E_NOTICE errors are displayed. If you can't change the php.ini file, try running ini_set('display_errors', 'On'); at the top of your script. Obviously, these errors will only show up and/or be logged if you trigger one, so you should double check that you're actually doing that somewhere.
One caveat is that the E_DEPRECATED error level was only introduced with PHP 5.3. When I just tested setting E_DEPRECATED on a PHP 5.2 install, PHP responded with errors, and set the error_reporting level to 0, which means it reports no errors at all. While it makes no sense for a pre-5.3 php.ini file to use this setting, I feel it's important to at least raise the possibility that you're using E_DEPRECATED on a server that doesn't support it. If you're not sure of your version, you can run phpinfo() and it will display a page with lots and lots of information, including the version number for your install.
Notwithstanding the above, if I've misunderstood your question and you're only talking about creating your own custom logging, then you need to create a function to run when an error occurs and assign it as the error handler using the set_error_handler() function.
It's important to note that when you use set_error_handler(), you bypass PHP's default error handler entirely. This means that your error_handler level becomes meaningless. All errors, regardless of their severity, will be passed to the error handler function you've created. The first parameter passed to this function by PHP will be the error number, which is the numeric value of the E_xxx constant of the error that was found. You'll need to write custom code to catch only the errors you want.
For example, to catch only E_NOTICE errors, your function would look like this:
function my_error_handler($errno, $errstr, $errfile, $errline) {
if ($errno == E_NOTICE) {
// handle/log the error
}
}
set_error_handler("my_error_handler");
Turns out the system has a custom Apache ErrorLog directive defined, and I have been tail -f ... the default Apache error_log.
Note to self: always check the web server setup first before posting silly questions on SO!
You can create your own handler
<?php
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if ($errno == E_USER_NOTICE){
/* log actions here */
}
}
set_error_handler("myErrorHandler");

PHP doesn't display any error

I don't get any PHP error, just get a white page on Firefox, and
Server error
The website encountered an error while retrieving http://example.com/pruebas/prov.php. It may be down for maintenance or configured incorrectly
on Chrome.
This is the code:
if (!ini_get('display_errors')) {
ini_set('display_errors', 1);
}
echo "hola"
echo "hola2";
I intentionally made mistake in the echo "hola" (there's no ';').
I also tried adding to the end of my .htaccess file -> suPHP_ConfigPath /home/username/public_html replacing username with my current username, and then I created a php.ini in public_html with "display_errors = on;". But I'm still not able to get any PHP error.
Your script is dying due to the syntax error before it ever executes, so the ini_set() call is never executed and never takes effect. You'd have to change the setting in the appropriate php.ini.
The actual error message may be in a log file somewhere. Try Apache's error_log, or see if PHP's logging somewhere else.
Make sure that you also have the appropriate error_reporting ini value set as well. You can find more information on PHP.net
Set the error reporting level. The parameter is either an integer representing a bit field, or named constants. The error_reporting levels and constants are described in Predefined Constants, and in php.ini. To set at runtime, use the error_reporting() function. See also the display_errors directive.
In PHP 4 and PHP 5 the default value is E_ALL & ~E_NOTICE. This setting does not show E_NOTICE level errors. You may want to show them during development.
Source
Make sure the display_errors is set to on on your php.ini file.

Can't suppress deprecated warnings in php v5.3

I've seemingly tried about every different suggestion provided through a couple hours of Google and stackoverflow searching to no avail, and I cannot seem to suppress a large sum of "Deprecated: Assigning the return value of new by reference is deprecated in " errors presented at the top of my application, as well as Many "Warning: the magic method __get() (and __set()) must have public visibility and cannot be static in . Thus far, I have added the following line and many different variations of it to my php.ini file:
error_reporting = E_ALL & ~E_DEPRECATED
error_reporting = E_ALL ^ E_DEPRECATED
I've also attempted a straight suppression of every single error:
error_reporting = ~E_ALL
To no avail either. I've confirmed that it is correctly reading the php.ini file by adjusting other settings successfully.
I've also applied the error_reporting() function (with all different variations of what's provided above) within my scripts with no more luck. Does the location of the reporting have anything to do with the suppression? I've tried posting it at the top of the first file being loaded, also at the top of a required file that is getting called immediately upon executing the main script, no where does it seem to take it.
Try it with a number: http://www.php.net/manual/en/errorfunc.constants.php
Everything but the two deprecateds would be 8191.
PS. It's possible the app/framework/website you're watching/editing/creating sets the error reporting level to E_ALL. If so, it doesn't matter what you set in php.ini, because it's overridden later.

Categories