Home  >  Article  >  Backend Development  >  php reports error 500 when calling its own method

php reports error 500 when calling its own method

(*-*)浩
(*-*)浩Original
2019-09-17 11:44:003238browse

For http request error status code 500, the usual explanation is: it means that the server encountered an error and was unable to complete the request (ie, internal server error), but the specific problem needs to be analyzed in detail.

php reports error 500 when calling its own method

PHP program syntax error causes

Scenario 1: Our project is set up with alarm monitoring (scheduled every 10 minutes Visit a fixed link on the website). There was a time when I would receive emails reporting 500 errors two or three times a day, but when I manually accessed it again, the access was normal... (Recommended learning: PHP Programming from entry to proficiency )

This should be the most common error, and grammatical errors can also reappear quickly. As long as the error message is exposed, the problem can be solved immediately.

If it is in a local or test environment, we usually handle it like this. Just set the output error message in the program entrance:

//error_reporting设置应该报告的错误,下面表示除了 E_NOTICE,报告其他所有错误
error_reporting(E_ALL ^ E_NOTICE);
//输出错误
ini_set('display_errors', 1);

But in the online environment, because users are using it , it is impossible to allow us to make such blatant printing errors, what should we do? You can set the error output to the log file in the program entry file. The specific code is as follows:

error_reporting(E_ALL ^ E_NOTICE);
//禁止把错误输出到页面
ini_set('display_errors', 0);
//设置错误信息输出到文件
ini_set('log_errors', 1);

//指定错误日志文件名
$error_dir = '/logs/err/';
$error_file = $error_dir . date('Ymd').'.log';
//目录不存在就创建
if (!is_dir($error_dir)){
    mkdir($error_dir, 0777, true);
}
//文件不存在就创建之
if(!file_exists($error_file)){
    $fp = fopen($error_file, 'w+');
    if($fp){
        fclose($fp);
    }
}

//设置错误输出文件
ini_set("error_log", $error_file);

//程序正常执行逻辑......

The problem in scenario 1 just mentioned was discovered later when we output it to the log in the same way as above. It was because mysql The connection was abnormally disconnected and the program continued to execute (it was normal when connecting to mysql, but an error was reported when the specific query method was called, remember it seemed to be where the method mysqli_real_escape_string() was used), which resulted in a fatal error, which was finally successfully repaired.

The above is the detailed content of php reports error 500 when calling its own method. 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