PHP 5 disable strict standards error - php

I need to setup my PHP script at the top to disable error reporting for strict standards.
Can anybody help ?

Do you want to disable error reporting, or just prevent the user from seeing it? It’s usually a good idea to log errors, even on a production site.
# in your PHP code:
ini_set('display_errors', '0'); # don't show any errors...
error_reporting(E_ALL | E_STRICT); # ...but do log them
They will be logged to your standard system log, or use the error_log directive to specify exactly where you want errors to go.

For no errors.
error_reporting(0);
or for just not strict
error_reporting(E_ALL ^ E_STRICT);
and if you ever want to display all errors again, use
error_reporting(-1);

All above solutions are correct. But, when we are talking about a normal PHP application, they have to included in every page, that it requires. A way to solve this, is through .htaccess at root folder.
Just to hide the errors. [Put one of the followling lines in the file]
php_flag display_errors off
Or
php_value display_errors 0
Next, to set the error reporting
php_value error_reporting 30719
If you are wondering how the value 30719 came, E_ALL (32767), E_STRICT (2048) are actually constant that hold numeric value and (32767 - 2048 = 30719)

The default value of error_reporting flag is E_ALL & ~E_NOTICE if not set in php.ini.
But in some installation (particularly installations targeting development environments) has E_ALL | E_STRICT set as value of this flag (this is the recommended value during development). In some cases, specially when you'll want to run some open source projects, that was developed prior to PHP 5.3 era and not yet updated with best practices defined by PHP 5.3, in your development environment, you'll probably run into getting some messages like you are getting. The best way to cope up on this situation, is to set only E_ALL as the value of error_reporting flag, either in php.ini or in code (probably in a front-controller like index.php in web-root as follows:
if(defined('E_STRICT')){
error_reporting(E_ALL);
}

In php.ini set :
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT

WordPress
If you work in the wordpress environment, Wordpress sets the error level in file wp-includes/load.php in function wp_debug_mode(). So you have to change the level AFTER this function has been called ( in a file not checked into git so that's development only ), or either modify directly the error_reporting() call

I didn't see an answer that's clean and suitable for production-ready software, so here it goes:
/*
* Get current error_reporting value,
* so that we don't lose preferences set in php.ini and .htaccess
* and accidently reenable message types disabled in those.
*
* If you want to disable e.g. E_STRICT on a global level,
* use php.ini (or .htaccess for folder-level)
*/
$old_error_reporting = error_reporting();
/*
* Disable E_STRICT on top of current error_reporting.
*
* Note: do NOT use ^ for disabling error message types,
* as ^ will re-ENABLE the message type if it happens to be disabled already!
*/
error_reporting($old_error_reporting & ~E_STRICT);
// code that should not emit E_STRICT messages goes here
/*
* Optional, depending on if/what code comes after.
* Restore old settings.
*/
error_reporting($old_error_reporting);

Related

Confusion regarding the elvis operator in PHP [duplicate]

I have a development version of PHP on Apache. I moved it to production and got this weird notices in my website. I don't have it on development version. How to enable these notices on my development version of website to fix them?
If you have access to your php.ini, then Björn answer is the way to go.
However, if you don't, or if you want to change a particular script / project error level, do this at the beginning of your code:
ini_set('display_errors', 1);
// Enable error reporting for NOTICES
error_reporting(E_NOTICE);
You can see which levels are available for error_reporting here: http://us2.php.net/manual/en/function.error-reporting.php.
It's always a good practice not to show any errors on production environments, but logging any weird behaviors and sending by mail to the administrator. NOTICES should only be enabled on development environments.
Change your php.ini file, the line that says error_reporting, to E_ALL.
I.e:
error_reporting = E_ALL
Seb is right, though you really should use constant for error_reporting().
error_reporting(E_NOTICE);
You can use bitwise operations to pick exactly the messages you want to display. For example:
// notices and warnings
error_reporting(E_NOTICE | E_WARNING);
// everything except errors
error_reporting(E_ALL ^ E_ERROR);

Turn off display errors using file "php.ini"

I am trying to turn off all errors on my website. I have followed different tutorials on how to do this, but I keep getting read and open error messages. Is there something I am missing?
I have tried the following in my php.ini file:
;Error display
display_startup_errors = Off
display_errors = Off
html_errors = Off
docref_root = 0
docref_ext = 0
For some reason when I do a fileopen() call for a file which does not exist, I still get the error displayed. This is not safe for a live website, for obvious reasons.
I always use something like this in a configuration file:
// Toggle this to change the setting
define('DEBUG', true);
// You want all errors to be triggered
error_reporting(E_ALL);
if(DEBUG == true)
{
// You're developing, so you want all errors to be shown
display_errors(true);
// Logging is usually overkill during development
log_errors(false);
}
else
{
// You don't want to display errors on a production environment
display_errors(false);
// You definitely want to log any occurring
log_errors(true);
}
This allows easy toggling between debug settings. You can improve this further by checking on which server the code is running (development, test, acceptance, and production) and change your settings accordingly.
Note that no errors will be logged if error_reporting is set to 0, as cleverly remarked by Korri.
You should consider not displaying your error messages instead!
Set ini_set('display_errors', 'Off'); in your PHP code (or directly into your ini file if possible), and leave error_reporting on E_ALL or whatever kind of messages you would like to find in your logs.
This way you can handle errors later, while your users still don't see them.
Full example:
define('DEBUG', true);
error_reporting(E_ALL);
if (DEBUG)
{
ini_set('display_errors', 'On');
}
else
{
ini_set('display_errors', 'Off');
}
Or simply (same effect):
define('DEBUG', true);
error_reporting(E_ALL);
ini_set('display_errors', DEBUG ? 'On' : 'Off');
In php.ini, comment out:
error_reporting = E_ALL & ~E_NOTICE
error_reporting = E_ALL & ~E_NOTICE | E_STRICT
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ER… _ERROR
error_reporting = E_ALL & ~E_NOTICE
By placing a ; ahead of it (i.e., like ;error_reporting = E_ALL & ~E_NOTICE)
For disabling in a single file, place error_reporting(0); after opening a php tag.
In file php.ini you should try this for all errors:
error_reporting = off
Let me quickly summarize this for reference:
error_reporting() adapts the currently active setting for the default error handler.
Editing the error reporting ini options also changes the defaults.
Here it's imperative to edit the correct php.ini version - it's typically /etc/php5/fpm/php.ini on modern servers, /etc/php5/mod_php/php.ini alternatively; while the CLI version has a distinct one.
Alternatively you can use depending on SAPI:
mod_php: .htaccess with php_flag options
FastCGI: commonly a local php.ini
And with PHP above 5.3 also a .user.ini
Restarting the webserver as usual.
If your code is unwieldy and somehow resets these options elsewhere at runtime, then an alternative and quick way is to define a custom error handler that just slurps all notices/warnings/errors up:
set_error_handler(function(){});
Again, this is not advisable, just an alternative.
In file php.ini you should try this for all errors:
display_errors = On
Location file is:
Ubuntu 16.04:/etc/php/7.0/apache2
CentOS 7:/etc/php.ini
You can also use PHP's error_reporting();
// Disable it all for current call
error_reporting(0);
If you want to ignore errors from one function only, you can prepend a # symbol.
#any_function(); // Errors are ignored
Turn if off:
You can use error_reporting(); or put an # in front of your fileopen().
I usually use PHP's built in error handlers that can handle every possible error outside of syntax and still render a nice 'Down for maintenance' page otherwise:
Format PHP error on production server
Open your php.ini file (If you are using Linux - sudo vim /etc/php5/apache2/php.ini)
Add this lines into that file
error_reporting = E_ALL & ~E_WARNING
(If you need to disabled any other errors -> error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE & ~E_WARNING)
display_errors = On
And finally you need to restart your APACHE server.
It is not enough in case of PHP fpm. It was one more configuration file which can enable display_error. You should find www.conf. In my case it is in directory /etc/php/7.1/fpm/pool.d/
You should find php_flag[display_errors] = on and disable it, php_flag[display_errors] = off. This should solve the issue.
It's been quite some time and iam sure OP's answer is cleared. If any new user still looking for answer and scrolled this far, then here it is.
Check your updated information in php.ini file
Using Windows explorer:
C/xampp/php/php.ini
Using XAMPP Control Panel
Click the Config button for 'Apache' (Stop | Admin | Config | Logs)
Select PHP (php.ini)
Can i create my own phpinfo()? Yes you can
Create a phpinfo.php in your root directly or anywhere you want
Place this <?php phpinfo(); ?>
Save the file.
Open the file and you should see all the details.
How to set display_errors to Off in my own file without using php.ini
You can do this using ini_set() function. Read more about ini_set() here (https://www.php.net/manual/en/function.ini-set.php)
Go to your header.php or index.php
add this code ini_set('display_errors', FALSE);
To check the output without accessing php.ini file
$displayErrors = (ini_get('display_errors') == 1) ? 'On' : 'Off';
$PHPCore = array(
'Display error' => $displayErrors
// add other details for more output
);
foreach ($PHPCore as $key => $value) {
echo "$key: $value";
}

MongoDB Codeigniter

I managed to run MongoDB on Codeigniter using Alex Bilbie` library. The operations go well (connection, queries etc. ) but sometimes I get these PHP notices:
Message: Mongo::__construct() [mongo.--construct]: localhost:27017: pool get (0x4bfab20)
Filename: libraries/Mongo_db.php
Line Number: 1274
A PHP Error was encountered
Severity: Notice
Message: Mongo::__construct() [mongo.--construct]: localhost:27017: found in pool (0x4bfab20)
Filename: libraries/Mongo_db.php
Is there a way to get rid of these? or maybe hide them..as they don't seem to mess up my pages in another way than splashing into the user's screen.
EDIT
On a few pages though, I use the JQgrid and when the errors show up they mess up my HTTP response and render some messy data.
The specific notice messages here have been removed in the MongoDB PHP driver 1.2.11 or later (see PHP-431). Upgrading your MongoDB driver to the latest should remove the excessive notice logging.
General notes on proper settings for Code Igniter logging in production still apply...
The E_NOTICE level of error reporting is intended to warn you about possible bugs or typos. It is typically only used as a development setting rather than for production messages.
The default error_reporting setting in PHP4 and PHP5 is actually set to ignore these:
E_ALL & ~E_NOTICE
The Code Igniter index.php has an application environment setting which normally shows all errors for development and suppresses error reporting for testing and production. You should set this appropriately:
define('ENVIRONMENT', 'production');
If you actually want to capture these messages for a production environment you should update the production settings in your index.php so that instead of error_reporting(0) you have:
error_reporting() set to appropriate level of detail
display_errors off
log_errors on
error_log path set
For example, in index.php you could have:
case 'production':
error_reporting(E_ALL);
ini_set('display_errors','Off');
ini_set('log_errors','On');
ini_set('error_log','/var/log/php5.log');
break;
That way the messages/notices will still be saved to the error_log if you want to review them, but they will not be shown to the end user.
Off the top of my head, there are a few things you could do. This is not an answer regarding a fix to the MongoDB error you are receveing, but rather regarding some methods in which you could hide the errors.
a) Use the '#' operator to ignore any error outputs from the method being called in which outputs the errors you are receiving. This would result in you renaming the method call to the following #method_outputting_mongodb_error($foo,$bar);.
b) Turn off error reporting in CodeIgniter. This is not the best method as all other errors will not be outputted.
There is a good link here re: how you can turn off error reporting: http://phpdog.blogspot.fr/2012/02/codeigniter-error-reporting-handling.html
As #Stennie mentions this has been fixed in later versions of the driver: https://jira.mongodb.org/browse/PHP-431
Try and upgrade.
Also this only existed on the Windows driver and only start happening after v1.2.1. I know that because the guy who made that JIRA also tried 1.2.1 on my advice and he didn't get those E_NOTICEs. I remember having the conversation that actually triggered that JIRA now :).
Unlike others I don't think you should "hide" these errors with E_ALL & ~E_NOTICE. No one is quite decided on hiding E_NOTICE however the general concensus is that it is not the best thing. Of course you would not want your logs to be filled with all this debug info about the MongoDB extension, instead the MongoDB extension should be silent (as you think it should).
Another reason I would not hide E_NOTICE is because of the php.ini of later versions of PHP:
; error_reporting
; Default Value: E_ALL & ~E_NOTICE
; Development Value: E_ALL | E_STRICT
; Production Value: E_ALL & ~E_DEPRECATED
By default PHP will actually install for Production Value (as it does for me) as such the default for PHP error reporting is:
E_ALL & ~E_DEPRECATED
This is the value I have for error_reporting on both my AWS and my local Ubuntu server 12.04 and I have not changed the values since installing as such the default value I gain from installing PHP from both repo and source is:
error_reporting = E_ALL & ~E_DEPRECATED
So the recommended default setup for production for later PHP versions (and probably future ones) actually shows E_NOTICE, which is useful but not if the logs are being filled with stuff from the Mongo extension.
Edit: Downvoter care to explain? I have basically given the same answer as #Stennie, why the downvote?
I have edited my answer to actually take the conversation in the comments on this answer and #Stennie's answer into consideration so the answer makes more sense now.

php, can't get warnings to appear on my test server, but in log file on my isps server?

I'm trying to get the warnings to show in my php error log.
My ISP has some warnings showing and I need to be able to see them on my test server.
error_reporting = E_ALL & ~E_NOTICE
display_errors = off
display_startup_errors = Off
log_errors = On
error_log = "C:\php-errors.txt"
I've upgraded my php version too 5.2.17
I also have
register_globals = Off
Like my ISP, but I can't get any warnings to show.
ini_set('display_errors', 'on'); is a great way to change php configurations settings specific for that page only. Include it in a global header/initialization file to make it application specific. Also, as mentioned before error_reporting(E_ALL); is good for this too.
Code at the top your scripts:
ini_set('display_errors', 'on');
error_reporting(E_ALL);
Be sure to use these only for development environments only.
Set display_errors to On to display the warnings in the page (instead of in the logfile) and set error_reporting to E_ALL | E_STRICT to display all warnings and errors in your php.ini.
If you want to show all possible warnings, try error_reporting(-1);, or you can put error_reporting = -1 into the php.ini file on your test server. It works, because the internal variable is used as a bit-field, and -1 sets all the bits on, hence, showing all possible errors.
To make sure that the error_reporting is still set to what you think, the variable returns the currently set level.
$prevErrLevel = error_reporting(-1);
echo "errlevel was: $prevErrLevel before setting to all.", __FILE__,':',__LINE__;

How to enable notices on my development server

I have a development version of PHP on Apache. I moved it to production and got this weird notices in my website. I don't have it on development version. How to enable these notices on my development version of website to fix them?
If you have access to your php.ini, then Björn answer is the way to go.
However, if you don't, or if you want to change a particular script / project error level, do this at the beginning of your code:
ini_set('display_errors', 1);
// Enable error reporting for NOTICES
error_reporting(E_NOTICE);
You can see which levels are available for error_reporting here: http://us2.php.net/manual/en/function.error-reporting.php.
It's always a good practice not to show any errors on production environments, but logging any weird behaviors and sending by mail to the administrator. NOTICES should only be enabled on development environments.
Change your php.ini file, the line that says error_reporting, to E_ALL.
I.e:
error_reporting = E_ALL
Seb is right, though you really should use constant for error_reporting().
error_reporting(E_NOTICE);
You can use bitwise operations to pick exactly the messages you want to display. For example:
// notices and warnings
error_reporting(E_NOTICE | E_WARNING);
// everything except errors
error_reporting(E_ALL ^ E_ERROR);

Categories