Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of 500 errors in php

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to solve the problem of 500th error in php". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to solve the problem of 500th error in php".

Php 500error solution: 1, check the PHP script and modify; 2, catch the exception and record the exception to the log; 3, analyze the log and deal with it.

This article operating environment: Windows7 system, PHP7.1 version, Dell G3 computer.

PHP and 500error

PHP development often encounters returning 500errors, and there is no debugging (available) in the body of the body. At this time, you need to debug slowly (break point, turn on debug mode, etc.), but if it is the current network, this error is more maddening, it is neither easy to break the point nor open debug mode. However, since it is a mistake, there will always be ways to deal with it. Here is a step-by-step analysis of the causes and solutions of the 500.

0x01, 500error

Internal Server Error, or internal service error, indicates that the service cannot process the request due to an unknown error. In PHP sites, it is usually returned by PHP, that is, 500errors are generally errors of PHP scripts.

Php-fpm grabs the bag 500.

As you can see from the figure above (Nginx+PHP-FPM schema), when PHP calls a class that doesn't exist, the script encounters an error and returns 500 to Nginx (and returns the error message, but uninstalls the STDERR).

0x02, what errors and exceptions will cause 500000

So what kind of error will lead to 500th error? all the error levels of PHP can be found in the official documents (http://php.net/manual/zh/errorfunc.constants.php) of PHP, and the error levels of E_ERROR, E_PARSE, E_RECOVERABLE_ERROR, E_USER_ERROR and uncaught exceptions will all cause 500errors.

500 caused by E_ERROR level error

0x03, under what circumstances the error will not return 500000

As mentioned above, this is caused by an error in the PHP script, but does an error or exception in the PHP script necessarily lead to 500? Obviously not, even if there is a fatal error in the script, you can still return 200.

Display_errors configuration options

In web applications based on python, nodejs, etc., if abnormal information occurs, it will be printed to the console (STDERR/STDOUT) by default. In PHP based on PHP-FPM architecture, there is no console to print, and its stderr and stdout are set to the corresponding STRDERR and STDOUT in FastCGI. If you redirect the error to STDOUT, the error is output directly to the response, and the status code is set to 200. This is also the capability implemented by the display_errors option.

The configuration of the display_errors option needs to be implemented through ini_set, and the configuration of display_errors in the PHP documentation indicates that the value is a string type, and the actual use of numeric and Boolean types can also turn the configuration on or off.

Error_reporting configuration

Display_errors controls whether the error details are displayed and whether the error status code is returned when an error occurs in the PHP script, while the error_reporting entry is used to control which level of error can be printed directly.

The settings for error_reporting can be configured through error_reporting (E_ALL) or ini_set ('error_reporting', E_ALL). For details of the function parameters, please refer to the PHP documentation.

It should be noted that PHP itself has an error log (error_log and log_errors configuration items). If an error occurs, PHP will write the correct error to the error log, and which errors need to be written are controlled by the error_reporting entry.

Do not display error details if the error level does not match

How to deal with 0x04 and current Network reasonably

The PHP script can not run properly, so all you can do at this time is to catch the exception and record it to the log to facilitate future debugging and processing of the current network bug.

PHP comes with error log

PHP itself already has a record of the error log. You can set the log_errors entry to On in php.ini and work with the error_log configuration item to specify the path where the error log is stored.

Error logging switch

Log path Settin

The writing of this error log is not controlled by the configuration of display_errors. In other words, errors are logged regardless of whether display_errors is turned on or not. However, it is controlled by the error_reporting configuration, and if the current error level does not match the error level in error_reporting, the error will not be written to the log. That is, if the error level is E_ERROR, but the setting is error_reporting (E_NOTICE), then the E_ERROR error message will not appear in the log.

The PHP error log records various types of errors

Log not written due to error level mismatch

Capture error exception record

PHP provides set_error_handler, register_shutdown_function, set_exception_handler, error_get_last and other related error handling functions. The error can be recorded by writing the captured error information to the specified log through the function.

For more information on the use of the function, please see http://km.oa.com/group/19368/articles/show/302491. A template is provided here:

$previousHandler = set_exception_handler (function (Exception $ex) use (& $previousHandler) {call_user_func ('exceptionHandler', $ex, $previousHandler);}); set_error_handler (' errorHandler'); register_shutdown_function ('fatalErrorHandler'); function exceptionHandler (Exception $ex, $previousHandler) {$info = array ($ex- > getFile (), $ex- > getLine (), $ex- > getCode (), $ex- > getMessage ()) / / logging logPHPError ($info); if (isset ($previousHandler) & & is_callable ($previousHandler)) {call_user_func ($previousHandler, $ex) }} / * Framework error handling function * @ param $errno * @ param $errstr * @ param $errfile * @ param $errline * @ return bool * / function errorHandler ($errno = 0, $errstr ='', $errfile ='', $errline = 0) {switch ($errno) {case E_WARNING: $errname = 'updated warning; break; case E_NOTICE: $errname =' updated attention' Break; case E_STRICT: $errname = 'Epistasis; break; case E_RECOVERABLE_ERROR: $errname =' Emission recovery errology; break; case E_DEPRECATED: $errname = 'Emission DEPRECATEDTER'; break; case E_USER_ERROR: $errname = 'Emission user error' Break; case E_USER_WARNING: $errname = 'Elastic warning; break; case E_USER_NOTICE: $errname =' ESERTERDEPRECATED; break; case E_USER_DEPRECATED: $errname = 'ESERTERDEPRECATED; break Default: restore_error_handler (); return false;} / / logging $info = array ($errfile, $errline, $errname, $errstr); logPHPError ($info); restore_error_handler (); return false } / * Fatal error error handling * / function fatalErrorHandler () {if (($e = error_get_last ()) & & $e ['type'] = E_ERROR) {$info = array ($e [' file'], $e ['line'],' line', $e ['message']) / / logging logPHPError ($info);}}

0x05 summary

To sum up, error_reporting is a function or configuration that controls the level of error information output to the browser or PHP error log, while display_errors controls whether error and alarm messages are output to the browser.

Because PHP's error log is global and controlled by error_reporting, it is recommended that you implement your own error (exception) capture logging logic in the business.

The above is all the content of this article "how to solve the problem of 500th error in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report