Home  >  Article  >  Backend Development  >  How to prevent the browser from prompting PHP error messages

How to prevent the browser from prompting PHP error messages

PHPz
PHPzOriginal
2023-04-03 14:09:38377browse

In PHP development, if there are errors in the code, the browser will prompt an error message, which will have an impact on the user experience and security of the website. Therefore, during development, it is necessary to prevent the browser from prompting PHP error messages to avoid this problem.

So, how to prevent the browser from prompting PHP error messages? Here are some methods.

1. Disable error reporting in the PHP.ini file

The PHP.ini file is the PHP configuration file, and the behavior of PHP can be controlled by modifying the file. We can set the error reporting level to 0 (turn off error reporting) in the PHP.ini file, so that error messages will not be prompted on the web page.

Open the PHP.ini file and find the following configuration item:

 ; error_reporting
 ; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
 ; Development Value: E_ALL
 ; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
 ; http://php.net/error-reporting
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED

Modify the last line to:

error_reporting = 0

After saving the modification, restart the web server to take effect.

2. Use PHP’s error control operator@

PHP’s error control operator@ to set all error messages to silent (that is, do not prompt error messages). You can use this in the PHP script Use this operator to achieve the effect of not prompting error messages.

The method of use is very simple, just add @ before the PHP function or variable, as shown below:

@mysql_connect(...)

When using this method, you need to pay special attention. If your code There is a serious error. Using this operator for modification only covers the error, but the problem still exists. If this method does not locate the error and causes security risks unknowingly, it is not advisable.

3. Use display_errors in the PHP.ini file

If the display_errors option is turned on, PHP will output error information during the execution of the script; if the display_errors option is turned off, all error information will be hidden. In this way, we You can hide irrelevant error information to avoid misleading users.

We can configure the display_errors option through the PHP.ini file. We can set the error message to be output to the log file, or we can directly turn off the option, as shown below:

; 默认为 on,错误信息输出到用户端
display_errors = off
; 错误信息输出到文件
error_log = /var/log/php-error.log

The above code will The display_errors option is turned off and error messages are output to the /var/log/php-error.log file.

Note: After modifying the PHP.ini file, you need to restart the web server to take effect.

4. Using PHP’s error_reporting function

By using the error_reporting function, we can dynamically configure the error reporting level in the script, turn off error reminders for some codes, and only set online transmission Record the error information for troubleshooting and improve the efficiency of error information troubleshooting.

The syntax of the error_reporting function is as follows:

int error_reporting([int $level])

Among them, the $level parameter indicates the error reporting level. It can be set to 0 to turn off error reporting, or other values ​​can be set to indicate setting specific errors. Reporting level.

5. Conclusion

The above methods have different uses and operation methods. You can choose the strategy that suits you according to your actual needs to prevent the browser from prompting PHP error messages. Effect. No matter which method is used, error information should be displayed when appropriate and necessary to facilitate timely troubleshooting and correction by programmers.

The above is the detailed content of How to prevent the browser from prompting PHP error messages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn