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 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!