Disallow PHP code overriding php.ini error_reporting - php

Can i disallow PHP override php.ini error_reporting settings? And take this settings only from php.ini.
php.init file have: error_reporting = E_ERROR|E_PARSE
PHP code have: error_reporting(E_WARNING|E_PARSE);
But this PHP line is in project core and i can't edit it and I don't need E_WARNING.

Can i disallow PHP override php.ini error_reporting settings?
You can disallow use of error_reporting() function in your code with use of disable_functions configuration directive. The downside is that you cannot have
disable_functions = error_reporting
set per vhost (i.e. via php_admin_value) but it must be set in main php.ini which may be problematic in some configurations.
Also I believe that your question exposed different problem and you are not fixing it here, but rather working around.

edit: just occured to me, you can use php runkit to do this instead of messing with the source code,
option 1:
install runkit ( https://github.com/zenovich/runkit / https://github.com/runkit7/runkit7 ), add runkit.internal_override=1 in php.ini, and run
runkit_function_rename("error_reporting","original_error_reporting");
runkit_function_add("error_reporting",function(int $ignored = NULL){return original_error_reporting();});
before running your intended code (you can also add this code in a file pointed to by the auto_prepend_file php.ini option to make sure it runs before any other code)
or option 2: edit the php interpreter source code,
in php-src/Zend/zend_builtin_functions.c find
/* {{{ proto int error_reporting([int new_error_level])
Return the current error_reporting level, and if an argument was passed - change to the new level */
ZEND_FUNCTION(error_reporting)
then right below that find
if (ZEND_NUM_ARGS() != 0) {
replace it with
if (0) {
then recompile PHP, and voila, error_reporting arguments are ignored :)
in git revision ab8094c666048b747481df0b9da94e08cadc4160 , which is 7.3.0-dev (slightly after 7.3.0-beta1), it is on line 736, see https://github.com/php/php-src/blob/ab8094c666048b747481df0b9da94e08cadc4160/Zend/zend_builtin_functions.c#L736

Related

I can't disable PHP errors

So what's going on is I tried
ini_set('display_errors', 'Off');
error_reporting(0);
Right below <?php, but this didn't seem to stop displaying them. So I went to the php.ini and went to display_errors and saw that it was set to Off. But it still showed.
So I went and did phpinfo() and display_errors along with display_startup_errors are both off. Also html_errors is off. I'm not sure if this will help, but it says error_reporting is set to -10241. Any ideas?
Do not change the value of error reporting to solve the issue. If display_errors is off, errors are not display independently of the error_reporting setting. This way you will not display errors but you can still log them.
The following should work:
ini_set('display_errors', 'Off');
If it doesn't work it could be that your server configuration does not allow you to change settings from PHP scripts. ini_set() returns FALSE on failure. So first of all you should check what value that call is returning. Make sure that ini_set is not listed among disabled PHP functions (disable_functions in php.ini).
If you are asking yourself why errors are still being displayed even if in php.ini the display_errors is Off, you can check the actual value of display_errors during the script execution:
ini_get('display_errors')
Pemember that PHP settings could be changed also in Apache host configuration and in .htaccess files. So check if you have an htacces that enables display_errors. Something like this:
php_flag display_errors on
Try to use:
ini_set('display_errors', 0);
ini_set('display_errors', false);
You don't describe what the errors are, so it's possible that your web server (Apache, nginx, etc) are what's throwing the error and not PHP.
If it is PHP, ensure that you're editing the correct php.ini as identified in your phpinfo.php. Remember that if you edit the php.ini, you will need to restart your PHP process (for example, on some *nix systems: service php-fpm restart. Your exact command may vary.)
If it's off in your php.ini, my guess is that it's being overridden somewhere else -- either later in the script ('grep "ini_set" /path/to/project/*.php' will find it). Also, the PHP Manual states that if the script has fatal errors, it doesn't apply if there are fatal errors:
Although display_errors may be set at runtime (with ini_set()), it
won't have any effect if the script has fatal errors. This is because
the desired runtime action does not get executed.

Why does $some_array['an-undefined-key'] throw an error on Mac but not Wamp?

For some reason my Mac is displaying the error message "Undefined index: an-undefined-key" whereas on Windows with WAMP it gracefully ignores the exception.
Example of usage:
if ($some_array['an-undefined-key']) {
// ...
}
Is there a PHP configuration which changes this behaviour?
I realise that I can do the following, but I would rather use the above technique if possible:
if (isset($some_array['an-undefined-key']) && $some_array['an-undefined-key']) {
// ...
}
Yes, you need to adjust the error_reporting and display_errors settings which sounds like they are different between your two different environments.
Here are links
error_reporting
PHP ini_set - look at first example for display_errors
The difference between the PHP on your Mac using the installed repo and your WAMP server is the php.ini, try editing your script and append the following lines to the top:
ini_set('display_errors', '1');
error_reporting(E_ALL);
This should be on your scripts. If this is not the problem, then please comment back and I will change accordingly.
You should not add unnecessary line to your script as you will have to change them when you go live.
Edit you php.ini ( using the wampmanager menus so you get the correct file )
left click wampmanager -> PHP -> php.ini
Look for these parameters and change to these values
error_reporting = E_ALL
This one makes error show on the web page
display_errors = On
error_log = "c:/wamp/logs/php_error.log"
this one will make the errors log into the file specified by error_log even if they dont show on the screen
log_errors = On
WAMP comes with XDEBUG configured so you should now see big orange errors in the web page
Check the bottom of the php.ini file for this line. Numbers for versions may differ on your system
; XDEBUG Extension
zend_extension = "c:/wamp/bin/php/php5.X.Y/zend_ext/php_xdebug-2.2.0-5.3-vc9.dll"

How can we enable php short-open-tag for a single script

Is it possible to enable php short-open-tag for a single script ??
The solutions that i have gone through mention adding short_open_tag=On in php.ini
or
php_value short_open_tag 1 to .htaccess
Can't we enable them under a php script like we enable error reporting..??
TL;DR - No, those are the only two options you have.
If you can't do any of the mentioned methods, you will need a container script that sets the value before including the script with short open tags.
<?php
ini_set('short_open_tag', 'On');
include 'myscript.php';
This will prevent a parse error in myscript.php due to short open tags.
The documentation isn't very clear about this, but apparently this stopped working since PHP 4 after which it can only be changed using .htaccess or editing php.ini. This excerpt seems to imply that it might work from 5.3 onwards:
PHP_INI_ALL in PHP 4.0.0. PHP_INI_PERDIR in PHP < 5.3.0
But that's not the case, as can be seen from answers of Cannot turn off short_open_tag with ini_set
I've lodged a bug report for this documentation issue.
Update
The documentation will be updated to reflect this behaviour more explicitly:
... it's been PHP_INI_SYSTEM | PHP_INI_PERDIR since 4.0.1.

Stop printing php error messages to browser

I'm using PHP 5.3, CentOS 6.2, httpd 2.2.15, NetBeans 7.0.1 (running remotely via ftp).
I want to stop printing error messages to the browser, it's enough that it prints to the error_log of httpd.
I thought by doing try/catch I would decide on my own how to handle the error but it still prints to both error_log and browser.
function smic_gettext($phrase){
try{
$tr_text = $this->language_array[$phrase];
} catch(Exception $e){
error_log("Couldn't find any entry in the translation file for ".$phrase.". ".$e);
return $phrase;
}
return $tr_text;
}
How should I configure in order to stop this behaviour?
I have tried setting display_errors=Off and display_errors=0 in php.ini. No difference (I did restart httpd).
display_errors = Off
in php.ini will let you keep your syslog errors, but write nothing to the browser.
You need to change the php.ini setting display_errors to off or 0. You can either do this in your actual php.ini, with a .htaccess file, or by calling this at the start of the script:
ini_set('display_errors', '0');
Try adding the following to the top of your script:
ini_set('display_errors',0);
This should set the error reporting to none and override the servers php.ini settings (which sometimes ignore your error_reporting(0) )
Wheter or not PHP errors are sent to the browser is determined by the php.ini setting: display_errors. Set it to Off to avoid it being output. This file is usually located under /etc/php.ini or /etc/php5/php.ini
If error appears only in one line it is possible to prevent error display with adding sign # to start of that line.
#YOUR_CUSTOM_COMMAND
Example:
#file_get_contents('custom_file.txt');
See display_errors directive
http://www.php.net/manual/en/errorfunc.configuration.php
If you want to hide errors and warnings, you can also set an error_handler.
See http://php.net/manual/function.set-error-handler.php
FWIW, while display_errors = off is the correct config-file line as others have said, on DreamHost (nd possibly other installations), it goes in
$HOME/.php/phprc
rather than php.ini (which might also work, but DreamHost -- and, again, possibly others -- supports phprc).

Controlling PHP's output stream

I want to keep errors out of my PHP output stream. I only want output of things I explicitly echo.
Looking at my php.ini, is "display_errors" the only configuration I need to change?
Instead of modifying php.ini, you can call this at a very early part of your code:
error_reporting(0);
Note that this means fatal errors will die silently as well, so it makes it a little difficult to debug at first.
I only recommend that if we're talking about a production machine. display_errors will hide them from the user, but make sure you have log_errors and error_log set in the php.ini so you'll see them on your regular log analysis (you do, right?).
For a development machine, I recommend keeping display_errors on and error_reporting(E_ALL | E_STRICT) so you'll see if anything is fishy.
You can modify that INI directive and change the flag to 0 (False) or disable error_reporting on a page by page basis
error_reporting(0)
Typically Production environments should be on display_errors = 0 Though not all of us have both Development and Production environments
You can also change the "verbosity" of the error messages by passing different values to error_reporting function (Or by changing the INI value for it in php.ini) More information on that can be found here: PHP: Runetime Configuration - error_reporting

Categories