of modifies PHP to mask errors

  • 2020-05-09 18:21:04
  • OfStack

There is a problem with learning the CI framework today:

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: user

1 as in ordinary PHP default file output 1 undefined declare variables is not wrong, but in the framework of codeigniter want to report an error, this to want to add and modify integration page 1 of the "lazy" is not very convenient, because it is a beginner started to also want to how this 1 errors in the code block. Even with the help of @, but listen to a lot of people say @ can significantly reduce performance... .

Finally, I suddenly thought to myself, "is codeigniter making this error message come out on purpose? How can we block this type of error?" I accidentally searched "how can codeigniter not display Notice information?" It was error_reporting(E_ALL) in the entrance index.php. Just change it to
error_reporting (E_ALL ^ E_NOTICE);
This error can be masked without affecting other error reporting.

Here's what I found:

error_reporting() sets the error reporting level of PHP and returns the current level.

grammar
error_reporting(report_level)
If the parameter level is not specified, the current error level is returned. The following are possible values for level:

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
2047 E_ALL
2048 E_STRICT
E_NOTICE means that 1 does not record normally and is only used if the program has an error, such as trying to access a nonexistent variable or calling stat() to view a nonexistent file.

E_WARNING is usually displayed without interrupting the program. This is very efficient for pair debugging. For example, call ereg() in the normal notation in question.

E_ERROR is usually displayed and program execution is interrupted. This mask cannot be traced to memory configuration or other errors.

E_PARSE parses errors from the syntax.
E_CORE_ERROR is similar to E_ERROR, but does not include errors caused by the PHP core.
E_CORE_WARNING is similar to E_WARNING, but does not include PHP core error warnings.

Error reporting for PHP
There are many configuration Settings in the php.ini file. You should have your php.ini file set up and put it in the appropriate directory, as shown in the documentation instructions for installing PHP and Apache 2 on Linux. When debugging an PHP application, you should be aware of two configuration variables. Here are the two variables and their default values:
display_errors = Off
error_reporting = E_ALL
By searching for them in the php.ini file, you can find the current default values for both variables. The purpose of the display_errors variable is obvious -- it tells PHP whether to display an error. The default value is Off. However, to make the development process easier, set this value to On:
display_errors = On
The default value of the error_reporting variable is E_ALL. This setting displays everything from bad coding practices to harmless tips to errors. E_ALL is a bit too fine-grained for the development process, because it also shows hints on the screen for small things (such as variables not being initialized), which can mess up the browser's output. I just want to see errors and bad coding practices, but I don't want to see harmless tips. So, replace the default value of error_reporting with the following:
error_reporting = E_ALL & ~E_NOTICE

Restart Apache and you're all set. Next, you'll learn how to do the same thing on Apache.

Error reporting on the server
Depending on what Apache is doing, opening error reporting in PHP may not work because there may be multiple versions of PHP on your computer. It is sometimes difficult to tell which version of PHP Apache is using, because Apache can only view one php.ini file. Not knowing which php.ini file is being used by Apache is a security issue. However, there is a way to configure the PHP variable in Apache to ensure that the correct error level is set.

Also, it's a good idea to know how to set these configuration variables on the server side to override or preempt the php.ini file to provide a higher level of security.
When configuring Apache, you should have already touched the basic configuration in the http.conf file in /conf/ httpd.conf.

To do what has already been done in the php.ini file, add the following lines to httpd.conf, overwriting any php.ini file:
php_flag display_errors on
php_value error_reporting 2039
This overrides the flag already set for display_errors in the php.ini file, as well as the value for error_reporting. The value 2039 represents E_ALL & ~E_NOTICE. If you prefer E_ALL, set the value to 2047. Again, restart Apache.
Next, test the error report on the server.

As for the function error_reporting(), it can shield some error information, but the errors caused by the PHP core cannot be shielded, because the errors caused by the PHP core will directly cause the compilation failure of PHP file. The errors caused by the writing format not written in accordance with the encoding rules of PHP cannot be shielded
 
* For now, avoid warnings of E_STRICT mode 
* (this must be done before function definitions) 
*/ 
if (defined('E_STRICT')) { 
$old_error_reporting = error_reporting(0); 
if ($old_error_reporting & E_STRICT) { 
error_reporting($old_error_reporting ^ E_STRICT); 
} else { 
error_reporting($old_error_reporting); 
} 
unset($old_error_reporting); 

The common ones are as follows:
 
// Turn off all error reporting; Close all errors  
error_reporting(0); 

// Report simple running errors; The report 1 A simple running error  
error_reporting(E_ERROR | E_WARNING | E_PARSE); 

// Reporting E_NOTICE can be good too (to report uninitialized 
// variables or catch variable name misspellings  ... ); Including the report 1 Uninitialized variables or catch misspelled variable names  
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; Report all errors but not all errors E_NOTICE  This is also php.ini Default Settings  
error_reporting(E_ALL ^ E_NOTICE); 

// Report all PHP errors (bitwise 63 may be used in PHP 3); Report all errors  
error_reporting(E_ALL); 

// Same as error_reporting(E_ALL); Same as above  
ini_set('error_reporting', E_ALL); 

Related articles: