Suppressing PHP undefined variable messages? - php

When I developed a website on my localserver it was working fine.
Now that I've uploaded it live I'm getting several notices.
Notice: Undefined index: ... on line 14
I've figured out that it happens because I'm using variables which arn't defined, and would like to go through and fix it. But I need a live version working tonight.
Is it possible to suppress the Notices and have the website act as it does on my localhost while its on my live server?

You've got it twice wrong. On your localhost and on your live server!
Localhost
Always show everything on screen, you want to know about notices too before you go live, as you can see now!
Live server
Never show anything on screen, it makes you vulnerable (it's deadly)
Log everything, also notices! So don't do what the other answers tell you!

You can choose which kind of errors will show up on your site on a global scale through php.ini or through .htaccess for specific folders, or per script by using error_reporting().
Read more on that and which options to set for your specific needs at www.php.net/manual/en/function.error-reporting.php
Also read: http://www.php.net/error-reporting

Look in the file php.ini for a line similar to error_reporting = E_STRICT - Edit it to remove the STRICT bit and put in error_reporting = E_ERROR.
I would recommend that in the near future that you fix those errors anyway.

Related

Notice warning from PHP in Symfony uploading file

I have a form that allows upload three files at the same time but just one is required. That works fine, my only problem is the following: if I upload three files I haven't any problem but if I upload one or two files (leaving two or one files empties) I obtain the following notice:
Notice: No file uploaded in Unknown on line 0
As much as empty files. The files are uploaded properly without any other problem, but I want remove that notice... or unless hide it, although I prefer remove it. I tried to hide it using
error_reporting(0);
and
ini_set('display_errors',0);
but neither of two worked...
It is the first time that I have problem, if someone could lead me I'd be very grateful due to that I am stuck with it.
If you are having the same problem as me, check with phpinfo() if you are using a debug version of PHP. If you see that Debug Build has a value of yes, your problem will be fixed if you install a live version of PHP instead of a debug version
The Error itself is caused by running a Debug version of PHP 7, see the bug report. As noted by HPierce because it was a Debug build it overrides the usual PHP settings for error_reporting. However as the Original question is actually about how to hide certain [expected] error messages (Notices), my answer is to this detail specifically.
Kevin, the attempted ways to hide errors you've listed in your question would normally work on non-debug PHP builds. However, it is unwise to ignore the errors, rather than solving them at source. It's also (more) unwise to hide all errors simply due to having expected errors appearing.
As it's only a Notice, you can work around it by setting your error_reporting() value as below:
//report all errors except notices.
error_reporting(E_ALL & ~E_NOTICE);
I would suggest this is far wiser than turning off error reporting entirely which is not recommended. If you want to stop errors being output to browser (as referenced by Tina) you can use display_errors.
Perhaps you may also need to set
ini_set('error_reporting', 0);
depending on your php ini configuration?
Also make sure you set it before carrying out any of the code.

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.

show error instead of "Server Error"

I have recently changed hosts, on my old host if i had an error in my syntax the error would be displayed (showing me where the error was)
On my new host i do not see this, i just see
The website encountered an error while retrieving http://www.XXX.co.uk/delete_product.php?q=66550. It may be down for maintenance or configured incorrectly.
Is there any way i can show the error instead of this?
Turn on error reporting.
Include these lines are the top of your script:
ini_set('display_errors',1);
error_reporting(E_ALL);
If you have access to edit the php.ini file, you can edit it and include the following option:
error_reporting = E_ALL
These settings will help you troubleshoot code faster and makes it easy to identify errors. However, it is not appropriate for a production-level use. You should use the first method and then you can remove the lines once you've fixed the issues. On local development environments, it's okay to edit php.ini file and add the directive as mentioned above.
On production systems, do not use ini_set('display_errors', 1); as it can show information you might want to keep hidden. Use the server's logs instead. By default apache for example logs these errors in error_log.
And, anything that is open to the general internet public is considered "production" in my opinion. Development means it is a server sitting in your own local network.
Turning on error reporting would work, but perhaps it would be better to look into the server logs.

Fatal PHP Errors in IIS 7.5

I'd like to see any PHP errors that are occuring, ie the "Expected ; on line 5 of myfile.php" sort of thing. Unfortunately, I cannot seem to figure out how to see this information.
I've set E_ALL, display_errors ON, friendly error messages are turned off, IIS is set to pass-through on errors, what am I missing?
Syntax errors used to show up as stated above on any page; they no longer do. We moved the server to a localhost for development, and I guess didn't mimic exactly the server config. Now I'm stumped.
Tried on IE and Chrome, neither of which show the errors.
Errors are logged in PHP's log file, but I'd still like them to be displayed on the page; at least for now.
UPDATE:
Just tried adding ini_set('display_errors', 'on'); directly into the requested page, and it now works.. but why? Why does it need to be set locally? My PHP.ini file has this declared already.
To answer the first part of the question; to see the errors when using ajax: You can use the developer tools of your browser to see the exact response from the server.
In FireBug for Firefox for example, you go to the Net tab and there you see all ajax request popping up as they happen. Opening one of these requests will give you an overview with more tabs like Response and HTML.
Try using:
error_reporting (-1);
E_ALL isn't really "all" for php < 5.4.
Also, make sure 'display_errors' is set.
ini_set( 'display_errors', 1 );
Well, looks like this is half my own stupidity, half the cloudiness of automatic installations.
Turns out there were TWO php.ini files, and that IIS used the one located within the iis express directory on the main drive, instead of the regular PHP directory.
So to anybody else having this problem, I'm providing the full list of crap you have to wade through to get the errors as you would like:
1) Turn off the IIS default error pages
2) Disable 'friendly error messages'
3) Ensure you are using the CORRECT php.ini file, and change the parameters as needed. Specifically error_reporting and display_errors.
All of this is necessary before seeing all of the error messages you need right in the browser.

IIS PHP doesn't log errors to log file

My local windows webserver (IIS) doesn't log php errors to log file.The php53_errors.log file is always empty. http://prntscr.com/2aels How to fix it? I think something goes wrong with permission settings. But it shows errors on browser window. And, one more question:Notices like "Undefined index".. Are they really important?
In the IIS server manager tool, there is option call error page. Please disable to use the IE error page to show you the error.
Besides please go to the C:\inetpub\wwwroot\web.config this file controls how to display and log the error as well. It should have one line like this.
<httpErrors errorMode="Detailed" />
Do you have php.ini in right setup according to this?
http://php.net/manual/en/install.windows.manual.php
specially keys log_errors and error_log
Read the last part of your question:
Notices like "Undefined index".. Are they really important?
It depends on your definition of 'important'.
The easy answer is no, they're not important. That's the reason they're classified as notices rather than warnings. It's okay to ignore them.
However you should still pay attention to them, and try to fix them where possible.
For example, the notice you quoted "Undefined index". This is caused (as I'm sure you're aware) by referencing an array element that hasn't yet been defined.
It is best practice to prevent this notice from being raised by using isset(), because if you do that, then when you do still get the notice, you'll know it is important - maybe you made a typo in your variable name or array index? The code will still run, but it won't work properly, and getting the notice might be the first clue you have of that. If that notice is suppressed or drowned out by legitimate ones, you'll never spot it.
Most 'notices' raised by PHP are on a similar level to that -- they indicate that something might be wrong, but PHP doesn't know for sure. If you write your code defensively to prevent notices from being raised when you're happy with the code, then you can afford to pay more attention to the ones that do still crop up.
Hope that helps.

Categories