这是一种为所有错误创建一个单独的错误页面,便于更新和维护的方法。
1) 将所有错误页面指向 .htaccess 文件中的一个位置
ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
etc.
2) 根目录下 error.php 页面的 PHP 代码
$status = $_SERVER['REDIRECT_STATUS'];
$codes = array(
403 => array('403 Forbidden', 'The server has refused to fulfill your request.'),
404 => array('404 Not Found', 'The document/file requested was not found on this server.'),
405 => array('405 Method Not Allowed', 'The method specified in the Request-Line is not allowed for the specified resource.'),
408 => array('408 Request Timeout', 'Your browser failed to send a request in the time allowed by the server.'),
500 => array('500 Internal Server Error', 'The request was unsuccessful due to an unexpected condition encountered by the server.'),
502 => array('502 Bad Gateway', 'The server received an invalid response from the upstream server while trying to fulfill the request.'),
504 => array('504 Gateway Timeout', 'The upstream server failed to send a request in the time allowed by the server.'),
);
$title = $codes[$status][0];
$message = $codes[$status][1];
if ($title == false || strlen($status) != 3) {
$message = 'Please supply a valid status code.';
}
// Insert headers here
echo '<h1>'.$title.'</h1>
<p>'.$message.'</p>';
// Insert footer here
相当棒!
对于静态或较少使用 CMS 的网站非常有用。
我经常为喜欢静态网站的客户使用它。
将一个 php 用于所有错误很不错,你可以一次性适应你的布局,如果你更改任何内容,会更容易…
非常适合所有网站…
非常有用,谢谢!
什么是静态网站?
很适合作为开始使用。
你在 .htaccess 中忘记了最重要的错误代码:错误 418。
小技巧,大效果 :) 好东西。
“数据库错误”是您从失败的论坛网站中看到的另一种错误… 可能与 SQL 相关,但我不确定。
$title = $codes[$status][0];
$message = $codes[$status][1];
我把代码放在根目录下的 error.php 中,导致出现未定义错误
对于标题和消息