Displaying php errors. Displaying errors at different levels in PHP


Let's figure out how to enable/disable the display of errors on the site. Through the admin panel and through the htaccess file.

Error display (Notice, Warning, Fatal Error) is important and required function, it's good that she exists! Otherwise, webmasters would spend months manually searching for errors in the code of their site.

How to enable error display from Joomla 3 admin panel?

To do this, in the Joomla admin panel go to “System - General Settings”.

Go to the "Server" tab, there you will see the "Error Messages" option. Select the "Maximum" mode.

Save.

After this, error messages, if any, will appear on the site pages. This is useful if somewhere on the site you see White screen.

After enabling error messages to appear on a white screen, you will see the type of error and its description.
Using this description, you can find a solution in a Yandex or Google search.

Enabling php error display via configuration.php

If you don’t have access to the admin panel or it doesn’t work, you can enable error display through the configuration.php file, which is located in the root folder of your site.

It contains a directive

public $error_reporting = "default";

To enable error display, change default to maximum

How to enable error display via .htaccess

If you do not have Joomla, but your own script, or for some reason you cannot enable error messages in the first two ways, then this can be done through the .htaccess file, which is also located in the root folder of your site.

Open .htaccess - pay attention to the dot in front. If you do not have such a file, then create it.
In CMS systems it is usually called htaccess.txt and can simply be renamed to .htaccess

At the very top add the line:

php_flag display_errors on

And the line:

php_flag display_errors off

turns off error display.

What kind of php error messages are there?

The most common are:

Notice- notification of minor errors in the code. Typically, the functionality of the site remains intact.

These notifications just appear in the header.

Warning- warning about more serious mistakes. Sometimes, with such a warning, the functionality of the site also remains, sometimes a white screen.

When working with a website, every developer has encountered a situation where, when enabling a module, adding his own code, or making other changes to the website, a so-called WHITE SCREEN OF DEATH(white screen of death) which is usually caused by an error PHP. The first action of the developer, naturally, is to roll back the changes that led to this error. But the error needs to be corrected, which means we need to look at what exactly causes it to occur.

It’s good if the developer has the opportunity to look at the Apache logs. But here difficulties arise: not every hoster maintains them, and not every programmer has access to them. In the end, to watch them you will have to go to the server. But all programmers are lazy, which means we need to look for a simpler solution. And this solution is to directly display errors on the screen.

I don't take into account the page in Drupal that allows you to display errors on the screen (admin/settings/error-reporting), because... It won't save you from a white screen. It displays errors that break modules, not the site as a whole. Those. if you forget to put it (for example) isset for a variable that may not be defined, an error will be shown on the screen. But if you forget to put a semicolon after performing the operation, then Drupal will no longer display this error, and you will see a white screen.

There are several ways to enable error output.

Method 1: Enabling error display via php.ini

If you have access (well, what if) to the server, then configuration file php.ini find the parameter error_reporting and set its value to E_ALL.

Error_reporting = E_ALL

Method 2. Enabling error display via .htaccess

At the root of Drupal there is a .htaccess file that regulates page loading processes. To enable messaging, open it and add the following two lines:

Php_flag display_errors on php_flag display_startup_errors on

Method 3. Enabling error display via index.php

Let's touch on the files in the Drupal root again, but this time index.php. Here, to display messages, you need to add error_reporting(7) before the line with the bootstrap.ini connection:

Error_reporting(7); require_once "./includes/bootstrap.inc" ;

Method 4: Enabling error display via settings.php

I’ll say right away that this method only works for Drupal 7. There is a settings.php file in the sites/default/ folder. Open it and add these lines:

Error_reporting(-1); $conf [ "error_level" ] = 2 ; ini_set ("display_errors" , TRUE ) ; ini_set ("display_startup_errors" , TRUE ) ;

When debugging scripts on PHP regular It's a matter of getting a "white screen" in the browser. Which in most cases indicates a stop PHP execution code due to an error. The PHP interpreter allows you to output official information about errors on the screen, which greatly facilitates debugging. But by default (in most cases), this behavior is disabled for security reasons, that is, PHP error messages are not displayed on the screen.

In this article I will tell you how to make PHP display error messages on the monitor screen in a browser window. The instructions are valid for the case when you are using the Apache web server and if PHP for your site is connected as an Apache module.

Displaying errors on the screen should only be enabled when debugging the site. The presence of such code can negatively impact the security of the web application.

Enabling PHP errors to be displayed on screen using the .htaccess file

This is very convenient way for debugging PHP code. Works in almost all cases. In the folder with the script on the site we place the .htaccess file with the following contents:

Php_flag display_errors on php_flag display_startup_errors on php_flag error_reporting E_ALL

  • display_errors- includes an option to display errors on the screen along with the rest of the code.
  • display_startup_errors- enables the option to display errors that occur when PHP starts when the display_errors directive is not yet working.
  • error_reporting- indicates which errors are displayed by significance level. When the directive is set to E_ALL all errors are displayed.

Enable PHP error output to screen in PHP file code

This method is convenient because you control the display of errors on the screen in the PHP script itself. Parameters set using the ini_set() function have higher priority and override directives php.ini And .htaccess. Place the following code at the beginning of the PHP file:

Ini_set("display_errors", 1); ini_set("display_startup_errors", 1); ini_set("error_reporting", E_ALL);

Enable PHP errors to be displayed on screen using the php.ini file

This method is relevant when you are a server administrator. In file php.ini edit following lines(add if necessary):

Display_errors = On display_startup_errors = On error_reporting = E_ALL

The best way to display PHP errors on screen

In my opinion ordinary user most convenient to use .htaccess, especially if you have more than one PHP file. Method number 2 is convenient for debugging one php file, so as not to affect the error output level for others php scripts. Option with php.ini Suitable only for server administrators, but its effect applies to all sites located on this server.

Acknowledgments

The following sources were used when writing this article.

No matter how careful and attentive a web programmer is, errors and inaccuracies are possible during his work. As a result, this may result in site or service failures. For debugging during project development, it is possible to enable error output in PHP.

How it works?

PHP certainly reacts to this or that script code. In some cases - by performing necessary action, in others it displays an error. This mechanism helps the developer to quickly correct defects at the time of implementing a section of code.

However, error output in PHP is not always necessary. After the development of the project is completed, this function is disabled in order to avoid hacking or unauthorized access.

Settings

For the entire configuration PHP interpreter The php.ini file is responsible. It contains the error_reporting directive, which determines the output of errors in PHP. However, although it includes exception handling, display_errors is responsible for displaying them in the browser window. If it is disabled, the system will display a blank page instead of an error.

Classification of exceptions

Error output in PHP can be divided into several categories:

    errors that will cause the script to stop running. The so-called fatal ones. 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 worth considering each type in more detail and describing its functionality.

    E_ERROR. Typically, this type refers to errors that cannot be quickly resolved or the script cannot be continued. These may include problems with memory allocation;

    E_WARNING. The code will continue to work, but a warning will be displayed indicating that there is some kind of error, the code of which is indicated in the message. Not critical;

    E_NOTICE. Notifications that indicate that something has happened that could cause an error. Also not critical to code execution;

    E_USER_ERROR. Errors generated by the user himself;

    E_ALL. This includes all types of errors. Usually, this option 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 used - on hosting or on 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 transform two directives - display_errors and error_reporting into 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 development is carried out on virtual hosting, then most often the error display function is disabled there for security reasons. Therefore, to activate it you will need to use the server settings file Apache htaccess. 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 display errors directly from the code using the ini_set() function. However, it is worth considering that after the development of the site is completed, its use may cause security problems.

Write to file

PHP allows you to save all errors that occur in specific place on your hard drive. To include PHP error output in a file, you can use three methods:

    edit the php.ini file. Here you need to define two lines. The first is log_errors = On, which actually activates the output option. 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 file.

    Use the ini_set function in the required place in the code.

Conclusion

Error output in PHP - necessary tool in code debugging. But it can also create potentially vulnerable areas. Therefore, you need to use this mechanism carefully and carefully. After a site or service has passed all stages of testing, you need to ensure that it does not display errors that could be used by an attacker to gain access to important data.

Today we will look at several methods for enabling error output in PHP.

Any, even the most experienced, programmer regularly makes mistakes in the code. Whether these are minor typos or serious failures in the algorithm, it doesn’t matter, tracking errors is a very important part of development. Below are several ways to track errors in PHP code.

Method one. Enable error output in php file.

You can enable error reporting directly in the actual file where you write the code. To do this, place the following lines at the beginning:

ini_set("display_errors", 1); ini_set("display_startup_errors", 1); error_reporting(E_ALL);

ini_set ("display_errors" , 1 ) ;

ini_set ("display_startup_errors" , 1 ) ;

error_reporting(E_ALL);

After that, just run the file, if there are errors in the code, they will be displayed on the page.

Method two. Enable display of errors in the php.ini file.

You can also enable error output in php configurations, then they will be displayed not only at startup specific file, and in all php files. To do this, find the php.ini file in file system your server and add the following lines to it:

error_reporting = E_ALL display_errors = On display_startup_errors = On

error_reporting = E_ALL

display_errors = On

display_startup_errors = On

After saving the changes to the file, reboot your server and the changes will take effect.

Method three. Enable error output in the .htaccess file.

Another way to enable errors is to make changes to the .htaccess file. To do this, find this file in your site's file system and add the following lines to it:

php_value display_errors 1 php_value display_startup_errors 1 php_value error_reporting E_ALL

php_value display_errors1

php_value display_startup_errors1

php_value error_reporting E_ALL

Be careful, as some CMS systems have another file called htaccess.txt, which serves as a template for making adjustments. You need to change the .htaccess file, otherwise the changes will not have any effect on the error output.

These three ways you can enable error output in your project written in PHP. However, remember that error output should only be enabled during development or when tracking a bug. Include this function When your project has already entered production, it is highly undesirable, I don’t think that your users will be happy when errors are displayed on the page instead of the desired content.