PHP-模拟403错误页面
我知道您可以发送一个标头,告诉浏览器禁止此页面,例如:
header('HTTP/1.0 403 Forbidden');
但是,如何显示在服务器上为此类错误创建的自定义错误页面呢?
默认情况下,仅发送标题会显示白页,但我记得有一阵子回读,您可以使用客户错误页面。 有人知道吗
12个解决方案
56 votes
发送标头后只需回显您的内容即可。
header('HTTP/1.0 403 Forbidden');
echo 'You are forbidden!';
alex answered 2019-11-17T08:54:44Z
32 votes
http_response_code是PHP 5.4中引入的,使事情变得更加容易!
http_response_code(403);
die('Forbidden');
Marcio Mazzucato answered 2019-11-17T08:55:09Z
20 votes
更改标题后,包括自定义错误页面。
Ibrahim AshShohail answered 2019-11-17T08:54:19Z
19 votes
为此,您必须首先为浏览器说用户收到错误403。为此,您可以使用以下代码:
header("HTTP/1.1 403 Forbidden" );
然后,脚本发送“错误,错误,错误,错误,错误.......”,因此您必须停止它。 您可以使用
exit;
通过这两行,服务器将发送错误并停止脚本。
别忘了:这可以模拟错误,但是您必须将其设置在.htaccess文件中,
ErrorDocument 403 /error403.php
Pyrrha answered 2019-11-17T08:55:53Z
6 votes
看到了很多答案,但正确的方法是按照php手册为标头函数调用提供完整选项
void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
如果您使用
header('HTTP/1.0 403 Forbidden', true, 403);
将遵循使用Apache或任何其他服务器配置的HTTP 403的正常行为。
Jiju Thomas Mathew answered 2019-11-17T08:56:31Z
4 votes
.htaccess
ErrorDocument 403 /403.html
answered 2019-11-17T08:56:56Z
3 votes
我已经阅读了所有答案,但都不是针对我所处情况的完整答案(在这个问题中完全相同),因此这是我如何收集建议答案的某些部分并提出确切解决方案的方法:
登陆服务器的真实403页面。 (转到服务器上的禁止访问的URL,或转到您喜欢的任何403页)
右键单击并选择“查看源代码”。 选择所有源并将其保存到您的域中的文件中,例如:[http://domain.com/403.html]
现在转到您的实际禁止页面(或php某些部分的禁止情况)示例:[http://domain.com/members/this_is_forbidden.php]
在任何HTML输出或标头之前回显以下代码! (即使是空格也会导致PHP发送HTML / TEXT HTTP标头,并且将无法使用)下面的代码应该是您的第一行!
$contents = file_get_contents('/home/your_account/public_html/domain.com/403.html', TRUE);
exit($contents);
现在您有了确切的解决方案。 我检查并验证了CPANEL最新访客,并将其注册为确切的403事件。
Tarik answered 2019-11-17T08:58:03Z
2 votes
为了最大程度地减少服务器的工作量,请使其变得简单:
.htaccess
ErrorDocument 403 "Forbidden"
PHP
header('HTTP/1.0 403 Forbidden');
die(); // or your message: die('Forbidden');
virtual_cia answered 2019-11-17T08:58:32Z
2 votes
使用Mod_Rewrite:
RewriteRule ^403.html$ - [F]
只要确保您在www根目录中创建一个名为“ 403.html”的空白文档,否则您将收到404错误而不是403错误。
Jay Sudo answered 2019-11-17T08:59:09Z
1 votes
我了解您的情况是您的apache conf或.htaccess中已经定义了ErrorDocument,并且希望通过php手动发送4xx状态代码时显示这些页面。
不幸的是,这对于常规方法是不可能的,因为php直接将标头直接发送到用户的浏览器(而不是Apache Web服务器),而ErrorDocument是从Apache生成的http状态的显示处理程序。
labemi answered 2019-11-17T08:59:41Z
-1 votes
发送403后刷新页面:
header('HTTP/1.0 403 Forbidden');
?>
Richard answered 2019-11-17T09:00:06Z
-3 votes
Alex,您可以使用以下标题重定向到您的自定义页面:
header('Location: my403page.html');
并确保在403页上包含原始标头代码:
header('HTTP/1.0 403 Forbidden');
另外,您可以只创建标题并包含403页面,如下所示:
header('HTTP/1.0 403 Forbidden');
include('my403page.html');
barners answered 2019-11-17T09:00:44Z