Can't silence imap_open error notices in PHP - php

I am using PHP 5.3.5 and I am using
$this->marubox=#imap_open($this->server,$this->username,$this->password);
The # sign should silence error reporting but it doesnt and I am sure that the error occurs on this line. I want my application to recognize the problem itself and react and get no NOTICE errors and I can't turn off error reporting for whole PHP because of my company development policy.
Without # i am getting:
imap_open() [function.imap-open]: Couldn't open stream {pop3.seznam.cz:110/pop3}INBOX
With it i get: Notice Unknown: Authentication failed (Authentication failed) (errflg=1)
If the login information is ok it opens the connection and no errors occur.
I always get NOTICE error when imap_open doesnt manage to connect and it's messing up with my JSON results. How to silence it please?

I added
$this->marubox=#imap_open($this->server,$this->username,$this->password);
imap_errors();
imap_alerts();
and imap_errors(); and imap_alerts(); do the magic :)

Two possibilities come to mind:
You could set error_reporting in your php.ini, ini_set or .htaccess or similar so that the NOTICE is suppressed, but since you wan't your application to handle the error, this is probably not, what you need
Implement your own error handling. This is not so difficult to do. You define a function for error hadnling and then tell PHP to use it instead of it's own default handler.
//define
function myHandler($errno, $errstr) {}
//somewhere towards the beginning of your processing script
set_error_handler("myHandler");
See set_error_handler for more. Also note that from the moment you register the handler, you're solely responsible. You can suppress or throw any errors you need/want.

Related

Why doesn't PHP show all of the syntax errors?

This is my error setting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL|E_PARSE);
Now I write this test code:
helloooooo;
And It shows me:
Use of undefined constant helloooooo ...
So far so good.
Now I remove the semicolon from end of my so-called code:
helloooooo
Shows nothing. It shows a white page.
In my experience whenever I see a white page there is a syntax error so I look for a typo.
The question is why doesn't PHP help in this case?
Someone said :
Syntax checking is done before executing any code. So it can't execute
the ini_set() functions if there's a syntax error.
How about PHP interpreter applies settings first then executes the rest of code?
JavaScript interpreter can detect the same error in runtime.
Just try this:
<script>
helloooooo
</script>
Now go to Firefox => Tools=> WebDeveloper => WebConsole
ReferenceError: helloooooo is not defined
PHP's default error reporting mechanism (might) suppress error output. If you're trying to turn it on at runtime using the error_reporting function, then PHP will first have to successfully parse the file, then execute it to set that new desired error reporting level. That fails when there are syntax errors, because then PHP can't successfully parse and execute the file to change the error reporting level. The syntax error is getting suppressed because… well… it couldn't change the error reporting level yet.
Javascript doesn't have that, it outputs any and all errors it encounters.
The reason PHP likes to suppress errors is because its running on the server, and internal server debug details shouldn't be shown on public web pages. In Javascript that is moot, since it's running in the browser anyway, and the errors are reported to the console, which regular users don't see. Moreover, PHP does log all errors to a log file, so the same way you go looking for errors in Javascript, you could explicitly go looking for them in PHP's log file as well.

php exception handling without error_reporting on

I'm working on a legacy app where towards the beginning of most files, php error reporting is disabled using error_reporting(0). The app is mostly functioning, but if I enable the error reporting there are a lot of errors and execution stops at the first one. When error reporting is disabled, errors are not logged anywhere. If I enable error reporting, error logging works great. Is there any way to enable error reporting without stopping execution of the script, as though error reporting was disabled? I need to log the errors but I also need the app to continue functioning as is.
No.
The very definition of an error is that the program has encountered a state which it cannot recover from. If it was recoverable/avoidable/non-destructive, then it would probably be a warning.
Exceptions are different, they are catchable and you can decide what to do with them with try/catch. I see a lot of code here where people simply ignore any caught exceptions and continue on as if nothing happened, but that is usually a big mistake and the reason they had a problem that warranted an SO post. Most exceptions should be handled by cleaning up your program [complete file writes, close handles/connections/etc] before displaying an error and halting execution.
Some of this may be accomplished with set_error_handler() but execution should still be halted at the end of your custom error handler.
Make sure this is in your php.ini file:
display_errors = Off
error_log = /var/log/php/error.log
log_errors = On

Suppress an error from the logs too

CodeIgniter 2.x still uses the classic mysql. We all know it's bad practice to still use it, but my job still requires me to use CodeIgniter.
I always have my Console.app (OSX) open watching my Apache/MySQL/PHP/CodeIgniter-native error logs.
Now I mostly have all notices/errors/etc. fixed always instantly when I see them and constantly monitor my development with Webgrind on my MAMP.
Back to the start; I constantly have one big annoying thing each page-load PHP always gives the error about mysql_pconnect is going to get deprecated in the future.
In the CodeIgniter driver the command is suppressed by # to not print the warnings to the screen, but it still ends up in my logs.
Is there any viable way to except one such error specifically in either PHP code or the PHP settings?
Locally I could recompile the whole PHP core and just remove the warning, but I would like to have the logs on our production installations rid of those warnings too :P.
Thanks in advance!
Traditionally, you can use set error verbosity using error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED) (i.e., report everything—except notices and deprecation warnings) as mentioned in "disabling deprecated errors".
Your issue may be related to CodeIgniter taking ownership of all errors.
system/core/CodeIgniter.php calls the function set_error_handler. You can find and modify the function _exception_handler it invokes on error in system/core/Common.php. It doesn't appear to be a configurable, so you may simply want to edit the line that begins with $is_error.

Suppress stacktrace with trigger_error when logging at E_USER_NOTICE

I want to log some messages from my php application in the apache error logs. But I would like this to happen only in the non-production environments.
I found that php has error_log to log messages to the error log file but how do I control it to only log if my application is running in the non-prod environments.
Also, I found that there is trigger_error which takes a error type parameter. And the error_reporting setting in php.ini can then decide which error types are actually logged. So, while in dev, I have error_reporting set to E_ALL , it is more restricted in production. So, now I can use trigger_log and always log at E_USER_NOTICE. This would mean that my message is only shown in the dev log files but not in the production log files.
However, trigger_error also produces a stack trace with every log which is a bit unnecessary for me. How can I disable this stack trace.
Also, am I doing things the right way. How do people generally handle this problem.
Edit:
I am not trying to suppress error messages. I am trying to provide more debug messages in dev. Things like "Request came with following parameters", "step 1 done", etc. This is really not required in production.
If you want just custom messages sent into error log manually, log_error() is the function you are looking for.
Firstly, suppressing error handling is not a good idea - errors have a huge impact on your application performance - and suppressing error reporting does not eliminate the slowdown.
There is no 'trigger_log' in PHP.
The build in error handling in PHP does not generate a stack trace - you must have a custom error handler installed. You need to find it and amend the code if you want to change it's behaviour.
Using 'trigger_error' to record debug events is a very bad idea.

PHP error reporting: some errors to screen, all to log

I want to know how I can display E_ERROR error messages to the screen but write E_ALL error messages to the error log, we currently use the error_reporting() in our app index page so we can change error reporting without the need to constantly restart the web server, but it seems that this (or perhaps the way it's meant to work) means that we only log errors that we see on the screen.
Is there a way to log and display different levels of errors?
Cheers!
You could make a custom error handler, and in your error handler check if the error is an E_ERROR; if so, print it out. Then log the error, regardless of whether it is an E_ERROR or not.
If you're not familiar with custom error handling, the PHP manual has a good example on how to use an error handler to do different things depending on the nature of a PHP error.

Categories