No matter how neat and careful the web programmer is, errors and inaccuracies are possible during his work. As a result, this may result in website or service failures. For debugging during project development, it is possible to enable error output in PHP.
How it works?
PHP certainly responds to a particular script code. In some cases - performing the necessary action, in others - it displays an error. This mechanism helps the developer to quickly correct defects at the time of implementation of the code section.
However, error output in PHP is not always needed. After the development of the project is completed, this function is disabled in order to avoid hacking or unauthorized access.
Customization
The php.ini file is responsible for the entire configuration of the PHP interpreter. It has the error_reporting directive, which just determines the error output in PHP. However, although it includes exception handling, display_errors is responsible for displaying them in a browser window. If it is disabled, the system will display a blank page instead of an error.
Exception Classification
Error output in PHP can be divided into several categories:
errors that cause the script to stop working. The so-called fatal. These include E_ERROR, E_COMPILE_ERROR;
errors that can be fixed. Their values ββfor the error_reporting directive can be: E_WARNING, E_NOTICE and others.
It is worthwhile to consider each type in more detail and describe its functionality.
E_ERROR Typically, this type refers to errors that cannot be quickly resolved or the script continues to run. This may include memory allocation problems;
E_WARNING. The code will continue its work, but a warning will appear that there is some kind of error, the code of which is indicated in the message. Not critical;
E_NOTICE. Notifications that show: something happened that could cause an error. Also not critical to code execution;
E_USER_ERROR. Errors generated by the user;
E_ALL. This includes all types of errors. As a rule, this option is enabled by default when installing the interpreter.
How to enable error output in PHP
The methodology for using error mechanisms in PHP may vary depending on where the code is applied - on a hosting or on a local computer. In the second case, the developer can configure his server and display as he pleases, namely, change the configuration in the php.ini file. It is enough to bring two directives - display_errors and error_reporting to the following form:
display_errors on
error_reporting E_ALL
These commands will allow you to display all error messages directly in the browser window.
If the development is carried out on a shared hosting, then most often the error display function is disabled there for security reasons. Therefore, to activate it, you will need to use the Apache htaccess server settings file. It is usually located at the root of the site. You need to add a couple of lines to it using any text editor:
php_flag display_errors on
php_value error reporting -1
You can also output errors directly from the code using the ini_set () function. However, it should be borne in mind that after the development of the site, its use can cause security problems.
Write to file
PHP allows you to save all errors that occur in a specific place on your hard drive. In order to include the output of PHP errors in a file, you can use three methods:
edit php.ini file. Two lines need to be defined here. The first is log_errors = On, which, in fact, activates the ability to output. The second is error_log = path / to the desired / file.
Change htaccess. You can also add two lines to it. php_value log_errors "on" and php_value error_log path / to the file.
Use the ini_set function in the required place in the code.
Conclusion
Error output in PHP is a necessary tool for debugging code. But it can also create potentially vulnerable areas. Therefore, you need to use this mechanism carefully and carefully. After the site or service has passed all stages of testing, you need to make sure that it does not display errors, with which the attacker can gain access to important data.