How to avoid or restrict PHP Error output - php

My PHP web-app sometimes returns this Fatal error on Line XXX due to reasons like extremely-slow-connection speed. Although, this occurs rarely, like 1 in 1000 times, I want to avoid such output on the browser. I am doing error logging on server side for some functions. So, I don't want to completely disable error reporting. Just that I don't want to display it to the end user.

Various Examples on turning off error_reporting..
<?php
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
?>
note : You can put or include() it in any file you dont want to explicit errors. Or if you want to totally off it. Go with tweaks in php.ini file.
Lines to find in your php.ini to tweaks and off error_reporting.
ini_set('display_errors', 'On'); //change to Off
error_reporting(E_ALL); // change to 0
FOR FURTHER INFO :: PHP.NET

To disable any error display to the user of your page, set
display_errors = Off
in your php.ini (this is the recommended setting for production websites anyway!) or
ini_set('display_errors', 0);
in your php.
This display_errors setting only affects the output on the webpage; it will not affect a possibly configured logfile; there the messages still would be logged.
See the php documentation on configuring error reporting: http://php.net/manual/en/errorfunc.configuration.php
Note: The error_reporting setting mentioned by other users here, will, to my knowledge, affect all kinds of error reports (i.e. also what is reported to a possibly configured log file). If you set error_reporting to 0, you won't get any log entries as well. If you want to log something to the log file but not show it to the user, the display_errors setting is the way to go.

on the start of that page write
error_reporting(0);

To avoid issues like this you can set the max_execution_time=1024M to avoid slow speed data generation and to hide errors is must likely error_reporting=0 on the php.ini file.
Or simply:
PHP Code
ini_set('max_execution_time',1024M);
error_reporting(0);
PHP Ini Settings
max_execution_time=1024M
error_reporting=0
Hope it helps.

error_reporting(0); only works if you are not using set_error_handler('my_function'). If this is your case, you have suppress the error message in 'my_function'.

Related

What's the purpose of ini_set() in php? (especially for error reporting)

Ok so PHP has the function ini_set() which a lot of people are aware of and will use to set various configuration options (here) to help with development etc. However, this function does only seem to work at runtime and will not work if there are any fatal errors or the script has syntax errors and can't be parsed / compiled.
Therefore surely there is no point of doing this (from the manual):
http://php.net/manual/en/function.ini-set.php
Examples
Example #1 Setting an ini option
<?php
echo ini_get('display_errors');
if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}
echo ini_get('display_errors');
?>
I don't know if I'm just missing something and my php.ini isn't configured correctly, but a lot of the time I get no errors. For beginners / juniors there will no doubt be a lot of syntax errors (missing semi-colons, closing brackets etc), and said juniors would search for how to turn on errors, assume the above manual entry is correct, yet when re-running their script, alas, they get no errors as the script cannot be parsed / compiled in the first place.
I know you can set display_errors = On in the php.ini file and restart your web server to show all errors to the screen (using this in a development environment, definitely not live), but wouldn't it be better just to remove the function and only configure the php.ini file for different error levels?
Update:
I know ini_set isn't just for displaying errors, but code can't be very manageable if you're calling ini_set in certain scripts / functions / files and wouldn't it make more sense to use the php.ini for something like that?
Update
So the ini file can be used to set global configuration options, surely you'd use this for security or optimisation, however developers could still use ini_set to override some of these options at runtime which may not be desirable
In summary (#Hanky웃Panky):
Why do I have the option of displaying errors when some trivial syntax errors will still not display?
yes, you are right that its better just to remove the function and only configure the php.ini file for different error levels.
But, this is good only that case when you have only one project in your machine, So, its all configuration setting you can do in php.ini
Consider, the case if you have multiple project setup. if you don't want some settings in that project still it will get from php.ini
So, it is suggested for some configuration settings you just set them at project level with ini_set() and will not reflect other projects.
string ini_set ( string $varname , string $newvalue );
The Purpose of ini_set is to set the value of the given configuration option.
This newvalue is kept by the configuration option during the script execution and restored at the scripts ending.
Example for setting an ini option
<?php
echo ini_get('display_errors');
if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}
echo ini_get('display_errors');
?>
string ini_set ( string $varname , string $newvalue )
Basically ini_set() sets the value of the given configuration option. The configuration option will keep this new value during the script's execution, and will be restored at the script's ending.
for all the variables which you can configure during the script run. please go through the below link.
Another settings can be configured at runtime using the the ini_set() function:
memory_limit and max_execution_time
(From ZCE test part about PHP Basics).
ini_set — Sets the value of a configuration option. Sets the value of the given configuration option. The configuration option will keep this new value during the script's execution, and will be restored at the script's ending, without ini_set(), values from php.ini file will be used.
EDIT:
You may find this helpful:
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

How to have PHP display errors? (I've added ini_set and error_reporting, but just gives 500 on errors)

So, I don't really have any errors in my current web page, but I want to be able to see an error when they pop up, instead of the HTTP 500 error page. I googled around a bit and thought adding these two lines would fix everything.
ini_set('display_errors', 'On');
error_reporting(E_ALL);
NOTE: I don't have access to the php.ini file, as I'm using my school account's server.
So I introduced a bug (no semicolon after $buggy) like so at the top of my page:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$buggy
$x = 4 + 2;
...
However, I just get a Server error:
"The website encountered an error while retrieving http://mywebpage.com/. It may be down for maintenance or configured incorrectly."
Any ideas?
EDIT:
I've reconfigured my code:
<?php
include_once 'database/errorSettings.php';
?>
<?php
$buggy // whoops
$x = 4 + 2;
...
errorSettings.php is the following:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
?>
But it still doesn't work... wrong way to reconfigure?
What you have is a parse error. Those are thrown before any code is executed. A PHP file needs to be parsed in its entirety before any code in it can be executed. If there's a parse error in the file where you're setting your error levels, they won't have taken effect by the time the error is thrown.
Either break your files up into smaller parts, like setting the error levels in one file and then includeing another file which contains the actual code (and errors), or set the error levels outside PHP using php.ini or .htaccess directives.
You need to set the error_reporting value in a .htaccess file. Since there is a parse error, it never runs the error_reporting() function in your PHP code.
Try this in a .htaccess file (assuming you can use one):
php_flag display_errors 1
php_value error_reporting 30719
I think 30719 corresponds to E_ALL but I may be wrong.
Edit Update: http://php.net/manual/en/errorfunc.constants.php
int error_reporting ([ int $level ] )
---
32767 E_ALL (integer)
All errors and warnings, as supported, except of level E_STRICT prior to PHP 5.4.0. 32767 in PHP 5.4.x, 30719 in PHP 5.3.x, 6143 in PHP 5.2.x, 2047 previously
Adding to what deceze said above. This is a parse error, so in order to debug a parse error, create a new file in the root named debugSyntax.php. Put this in it:
<?php
/////// SYNTAX ERROR CHECK ////////////
error_reporting(E_ALL);
ini_set('display_errors','On');
//replace "pageToTest.php" with the file path that you want to test.
include('pageToTest.php');
?>
Run the debugSyntax.php page and it will display parse errors from the page that you chose to test.
Just write a following code on top of PHP file:
ini_set('display_errors','on');
Syntax errors is not checked easily in external servers, just runtime errors.
What I do? Just like you, I use
ini_set('display_errors', 'On');
error_reporting(E_ALL);
However, before run I check syntax errors in a PHP file using an online PHP syntax checker.
The best, IMHO is PHP Code Checker
I copy all the source code, paste inside the main box and click the Analyze button.
It is not the most practical method, but the 2 procedures are complementary and it solves the problem completely
I have had this problem when using PHP5.4 and Plesk 11.5
Somehow, the error reporting and display error settings in the Plesk domain configuration page were completely overriding any local settings in .htaccess or the PHP scripts. I have not found a way to prevent this happening, so use the Plesk settings to turn error reporting on and off.
You may have settings in your php.ini that prevents the local site from overriding these settings, perhaps enforced by the control panel used on your server.
To people using Codeigniter (i'm on C3):
The index.php file overwrite php.ini configuration, so on index.php file, line 68:
case 'development':
error_reporting(-1);
ini_set('display_errors', 1);
break;
You can change this option to set what you need. Here's the complete list:
1 E_ERROR
2 E_WARNING
4 E_PARSE
8 E_NOTICE
16 E_CORE_ERROR
32 E_CORE_WARNING
64 E_COMPILE_ERROR
128 E_COMPILE_WARNING
256 E_USER_ERROR
512 E_USER_WARNING
1024 E_USER_NOTICE
6143 E_ALL
2048 E_STRICT
4096 E_RECOVERABLE_ERROR
Hope it helps.

No PHP errors on browser. what config is responsible for silent failure?

I was trying to run a simple PHP script on Amazon EC2.
As i got blank screen on the browser, started putting some garbage syntax or echo in between steps. Then i figured out that script was failing without any error.
How to disable silent failure?
<?php
putenv('HOME=/root');
echo 'after env'; //displayed on browser
require_once('/home/ec2-user/AWSSDKforPHP/sdk.class.php');
//i believe this require step was failed
echo 'after require'; // not displayed on browser
$ec2 = new AmazonEC2();
$response = $ec2->describe_availability_zones();
print_r($response);
echo 'hello';
?>
It can depend on your php.ini settings, error display or error reporting might be off.
Add this to the top of your script:
ini_set('display_errors', 1);
error_reporting(E_ALL);
You can use the same answer from Wesley Murch
Add this to the top of your script:
ini_set('display_errors', 1);
error_reporting(E_ALL);
Above you don't need to do anything, just refresh your php page.
or edit the php.ini file at "/etc/php5/apache2/php.ini"
and add or edit the line
display_errors = On
If you edit the php.ini file, you will need to restart apache.
(sometimes it doesn't work, and then you should use the first/local solution)
Ubuntu:
sudo service apache2 restart
I hope it works.
Try one of the following:
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE); # this is what you might want to try using
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
I had this problem and fixed it by editing /etc/php.ini to display_errors = On

On the fly error reporting in PHP

When our site used to be on IIS hosting with PHP installed, I had error reporting set to E_NONE and was able to turn it on temporarily by using:
ini_set('display_errors', 1);
That command seems to no longer work now that we are on Linux/Apache hosting. I have tried purposely sending bad commands to the server and I get no errors reported.
What am I doing wrong? Is there any other way to temporarily turn on error reporting without having to edit the php.ini each time?
You can change error reporting to E_ALL using the following line:
error_reporting(E_ALL);
Try adding that to the file.
The best way to turn on all errors is:
error_reporting( -1 );
This is better than E_ALL, as E_ALL doesn't actually mean all errors in all versions of PHP (it only does in the most recent). -1 is the only way to ensure it's on in all cases.
I just had to do this in one of my scripts. DOMDocument warnings were killing my logs. So, here's what you do:
// First, grab a copy of the current error_reporting level
// while setting the new level, I set it to zero because I wanted
// it off - but you could easily turn it on here
$erlevel = error_reporting(0);
// Then, do stuff that generates errors/warnings
// Finally, set the reporting level to it's previous value
error_reporting($erlevel);

Is there a way that I enforce that PHP reports error if I use an uninitialized/undefined variable?

I made a huge mistake by mixing result with results and it took me around 4 hours to finally find the bug.
So here is the question, in PHP, is it possible that I can enforce PHP to report errors if I use an undefined/uninitialized variable.
thank you
Set error reporting to E_ALL and ensure that display_errors in php.ini is on.
php.ini
display_errors = On
PHP code
// If you cannot access the php.ini file
// you can do this within your PHP code instead
#ini_set('display_errors' '1');
error_reporting(E_ALL);
The default setting you have right now probably excludes notices, the kind of errors PHP raises on uninitialized variables, which could be something like this:
error_reporting(E_ALL & ~E_NOTICE);
In a development environment I prefer using error_reporting(-1). Which reports all PHP errors.
yes, use error_reporting() and set it to E_ALL, like this:
error_reporting(E_ALL);
Set error reporting to report all errors. Either in php.ini or at runtime using error_reporting(E_ALL)
it already does report an error. something like this:
"Notice: Undefined variable: a in C:\wamp\www\testcenter\index.PHP on line 40"
maybe you didn't go specific enough. but you should try error_reporting(-1); as as if enforces the php to show some recomendations. a piece from the php manual about E_STRICT errors:
Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.
just remember that error_reporting(-1); shows more errors than error_reporting(E_ALL); because E_STRICT errors are not included in the E_ALL constraint.

Categories