Home > Backend Development > PHP Tutorial > PHP program access report 500 error handling plan

PHP program access report 500 error handling plan

慕斯
Release: 2023-04-10 09:38:02
forward
4216 people have browsed it

We have learned so much about PHP, but I don’t know if you have fully mastered the syntax errors of PHP programs. Yes, if not, then follow this article to continue learning

PHP program syntax error caused

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

This should be the most common error, and syntax 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, this is usually how we handle it. 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);
Copy after login

But in an online environment, because all users are using it, it is impossible. What should we do if we are allowed to make such blatant printing errors? 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);

//程序正常执行逻辑......
Copy after login

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 disconnected abnormally 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 that it seemed to be a fatal error when the method mysqli_real_escape_string() was used), and finally everything went smoothly. Fixed.

The disk is full causing

# Scenario 2: Once, a colleague said that the picture could not be uploaded and kept reporting error 500. It was working fine before, but I couldn't find out what the problem was. Because I was responsible for the development of that area at the time, so I came to see me. Various checks found no problem. Other pages were accessible normally. I deleted all the code on this page. I still get an error after uploading again. After searching for a long time, I finally found out that the disk is full o(╥﹏╥)o...

The 500 error message is caused by insufficient disk space and insufficient space to read and write data. It is uncommon and difficult to detect in time. Usually when encountering the 500 error, the first thing that comes to mind is a program error. In fact, it may also be due to insufficient disk space. If you find any problems in the program, you can check whether it is due to insufficient disk space~
df -h Check the disk space usage
PHP program access report 500 error handling plan

##For the http request, the error status code is 500, The usual explanation is:

means that the server encountered an error and was unable to complete the request (i.e., an internal server error) , but the specific problem must be analyzed in detail

Recommended study: "

PHP Video Tutorial

The above is the detailed content of PHP program access report 500 error handling plan. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template