Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP7显示自定义错误页面-详细信息供我使用,普通用户使用_Php_.htaccess_Error Handling_Php 7 - Fatal编程技术网

使用PHP7显示自定义错误页面-详细信息供我使用,普通用户使用

使用PHP7显示自定义错误页面-详细信息供我使用,普通用户使用,php,.htaccess,error-handling,php-7,Php,.htaccess,Error Handling,Php 7,使用Linux/Apache/PHP7,我希望优雅地显示所有错误消息。我的.htaccess文件包含以下内容: ErrorDocument 400 /errors/error.php ErrorDocument 401 /errors/error.php ErrorDocument 403 /errors/error.php ErrorDocument 404 /errors/error.php ErrorDocument 500 /errors/error.php ErrorDocument

使用Linux/Apache/PHP7,我希望优雅地显示所有错误消息。我的.htaccess文件包含以下内容:

ErrorDocument 400 /errors/error.php
ErrorDocument 401 /errors/error.php
ErrorDocument 403 /errors/error.php
ErrorDocument 404 /errors/error.php
ErrorDocument 500 /errors/error.php
ErrorDocument 502 /errors/error.php
ErrorDocument 504 /errors/error.php
$status = $_SERVER['REDIRECT_STATUS'];
$codes = array(
400 => array('400 Bad Request', 'The request cannot be fulfilled due to bad syntax.'),
403 => array('403 Forbidden', 'The server has refused to fulfil your request.'),
404 => array('404 Not Found', 'The page you requested was not found on this server.'),
405 => array('405 Method Not Allowed', 'The method specified in the request 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 while trying to carry out 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 HTTP status code.';
}
echo '<h1>Hold up! '.$title.' detected</h1>
<p>'.$message.'</p>';
…它指向带有以下内容的error.php页面:

ErrorDocument 400 /errors/error.php
ErrorDocument 401 /errors/error.php
ErrorDocument 403 /errors/error.php
ErrorDocument 404 /errors/error.php
ErrorDocument 500 /errors/error.php
ErrorDocument 502 /errors/error.php
ErrorDocument 504 /errors/error.php
$status = $_SERVER['REDIRECT_STATUS'];
$codes = array(
400 => array('400 Bad Request', 'The request cannot be fulfilled due to bad syntax.'),
403 => array('403 Forbidden', 'The server has refused to fulfil your request.'),
404 => array('404 Not Found', 'The page you requested was not found on this server.'),
405 => array('405 Method Not Allowed', 'The method specified in the request 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 while trying to carry out 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 HTTP status code.';
}
echo '<h1>Hold up! '.$title.' detected</h1>
<p>'.$message.'</p>';
$status=$\u服务器['REDIRECT\u status'];
$codes=数组(
400=>array('400错误请求','由于语法错误,无法完成请求'),
403=>array('403禁止','服务器已拒绝满足您的请求'),
404=>array('404未找到','在此服务器上未找到您请求的页面'),
405=>array('405方法不允许','请求中指定的方法不允许用于指定的资源'),
408=>array('408请求超时','您的浏览器未能在服务器允许的时间内发送请求'),
500=>array('500内部服务器错误','由于服务器遇到意外情况,请求未成功'),
502=>array('502坏网关','服务器在尝试执行请求时收到无效响应'),
504=>array('504网关超时','上游服务器未能在服务器允许的时间内发送请求'),
);
$title=$code[$status][0];
$message=$code[$status][1];
如果($title==false | | strlen($status)!=3){
$message='请提供有效的HTTP状态代码';
}
echo‘等等!’$检测到“标题”
“.$message”。

”;
404函数似乎工作正常,但任何php错误(如函数名中的输入错误)仍然显示为带有标准php错误消息的白色屏幕。即使在.htaccess文件中故意输入错误,也会产生一个通用的500错误,但不会使用我为此目的创建的error.php页面


我还想做的是为我显示php错误详细信息,但为用户隐藏详细信息,即使在生产模式下也是如此。

这听起来可能很糟糕,但“嗯哼”——您已经告诉它如何处理HTTP错误(其中404是最容易测试的),但您还没有告诉它如何处理php错误。查看
set\u exception\u handler
set\u error\u handler
(在PHP7之前),以及
register\u shutdown\u function
以查看是否发生了错误。尽管如此,如果您的错误处理程序中出现错误,那么所有赌注都将被取消,毫无疑问,您将以暴露的PHP错误告终。
.htaccess
中的错误将使
.htaccess
无效,因此无法处理。