我正在我的网站中实现一个数据库并了解 http 状态代码。我正在尝试为请求可能引起的任何问题实施解决方案,并且我考虑在连接到数据库以更新或查询它时,如果服务器端不存在该文件,正确的状态代码是什么(它可能已被暂时删除以停止传输任何数据?)。我将向用户实施自定义消息以通知他们,但想知道此响应的“正确”状态代码是什么?我已将其缩小到 404(未找到文件)或 503(服务不可用)或其他我不知道的?
PHP代码如果有帮助
try {
if (!file_exists("dbConn.php")) {
throw new Exception("File Not Found");
} else {
require_once("dbConn.php");
$dbConn = getConnection();
}
// other code
} catch (Exception $e) {
if ($e->getMessage() == "Bad Request") {
http_response_code(400); // This is for an exception thrown in 'other code' (not displayed)
} else {
// http_response_code(404) or http_response_code(503) or other?
}
}
largeQ