php 模拟 403 错误页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5061675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:41:50  来源:igfitidea点击:

Emulate a 403 error page

phpheaderhttp-status-code-403

提问by NightHawk

I know you can send a header that tells the browser this page is forbidden like:

我知道你可以发送一个标题,告诉浏览器这个页面是被禁止的,比如:

header('HTTP/1.0 403 Forbidden');

But how can I also display the custom error page that has been created on the server for this type of error?

但是我怎样才能显示在服务器上为这种类型的错误创建的自定义错误页面?

By default, just sending the header displays a white page, but I remember a while back reading that you can use the customer error page. Does anybody know?

默认情况下,只发送标题会显示一个白页,但我记得前段时间读到您可以使用客户错误页面。有人知道吗?

采纳答案by Ibrahim AshShohail

Include the custom error page after changing the header.

更改标题后包含自定义错误页面。

回答by alex

Just echo your content after sending the header.

发送标头后只需回显您的内容。

header('HTTP/1.0 403 Forbidden');

echo 'You are forbidden!';

forbidden

禁止的

回答by Marcio Mazzucato

http_response_codewas introduced in PHP 5.4 and made the things a lot easier!

http_response_code是在 PHP 5.4 中引入的,让事情变得更容易了!

http_response_code(403);
die('Forbidden');

回答by Pyrrha

For this you must first say for the browser that the user receive an error 403. For this you can use this code:

为此,您必须首先对浏览器说用户收到错误 403。为此,您可以使用以下代码:

header("HTTP/1.1 403 Forbidden" );

Then, the script send "error, error, error, error, error.......", so you must stop it. You can use

然后,脚本发送“错误,错误,错误,错误,错误......”,所以你必须停止它。您可以使用

exit;

With this two lines the server send an error and stop the script.

通过这两行,服务器发送错误并停止脚本。

Don't forget : that emulate the error, but you must set it in a .htaccess file, with

不要忘记:模拟错误,但您必须将其设置在 .htaccess 文件中,使用

ErrorDocument 403 /error403.php

回答by Jiju Thomas Mathew

Seen a lot of the answers, but the correct one is to provide the full options for the header function call as per the php manual

看到了很多答案,但正确的答案是根据php手册为标头函数调用提供完整选项

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )

If you invoke with

如果你调用

header('HTTP/1.0 403 Forbidden', true, 403);

the normal behavior of HTTP 403 as configured with Apache or any other server would follow.

使用 Apache 或任何其他服务器配置的 HTTP 403 的正常行为将遵循。

回答by Tarik

I have read all the answers here and none of them was complete answer for my situation (which is exactly the same in this question) so here is how I gathered some parts of the suggested answers and come up with the exact solution:

我已经阅读了这里的所有答案,但没有一个是针对我的情况的完整答案(在这个问题中完全相同)所以这里是我收集建议答案的某些部分并提出确切解决方案的方法:

  1. Land on your server's real 403 page. (Go to a forbidden URL on your server, or go to any 403 page you like)
  2. Right-click and select 'view source'. Select all the source and save it to file on your domain like: http://domain.com/403.html
  3. now go to your real forbidden page (or a forbidden situation in some part of your php) example: http://domain.com/members/this_is_forbidden.php
  4. echo this code below beforeany HTML output or header! (even a whitespace will cause PHP to send HTML/TEXT HTTP Header and it won't work) The code below should be your first line!

        <?php header('HTTP/1.0 403 Forbidden');
        $contents = file_get_contents('/home/your_account/public_html/domain.com/403.html', TRUE);
        exit($contents);
    
  1. 登陆您服务器的真实 403 页面。(转到您服务器上的禁止网址,或转到您喜欢的任何 403 页面)
  2. 右键单击并选择“查看源代码”。选择所有源并将其保存到您的域中的文件中,例如:http: //domain.com/403.html
  3. 现在转到您真正的禁止页面(或 php 某些部分的禁止情况)示例:http: //domain.com/members/this_is_forbidden.php
  4. 任何 HTML 输出或标题之前回显以下代码!(即使是空格也会导致 PHP 发送 HTML/TEXT HTTP 标头,它不会工作)下面的代码应该是你的第一行

        <?php header('HTTP/1.0 403 Forbidden');
        $contents = file_get_contents('/home/your_account/public_html/domain.com/403.html', TRUE);
        exit($contents);
    

Now you have the exact solution. I checked and verified with CPANEL Latest Visitors and it is registered as exact 403 event.

现在您有了确切的解决方案。我检查并验证了 CPANEL 最新访问者,它被注册为精确的 403 事件。

回答by Tarik

.htaccess

.htaccess

ErrorDocument 403     /403.html

回答by virtual_cia

To minimize the duty of the server make it simple:

为了最小化服务器的职责,让它变得简单:

.htaccess

.htaccess

ErrorDocument 403 "Forbidden"

PHP

PHP

header('HTTP/1.0 403 Forbidden');

die(); // or your message: die('Forbidden');

回答by Jay Sudo

Use ModRewrite:

使用 ModRewrite:

RewriteRule ^403.html$ - [F]

RewriteRule ^403.html$ - [F]

Just make sure you create a blank document called "403.html" in your www root or you'll get a 404 error instead of 403.

只需确保在 www 根目录中创建一个名为“403.html”的空白文档,否则您将收到 404 错误而不是 403。

回答by labemi

I understand you have a scenario with ErrorDocument already defined within your apache conf or .htaccess and want to make those pages appear when manually sending a 4xx status code via php.

我知道您的 apache conf 或 .htaccess 中已经定义了一个 ErrorDocument 场景,并且希望在通过 php 手动发送 4xx 状态代码时显示这些页面。

Unfortunately this is not possible with common methods because php sends header directly to user's browser (not to Apache web server) whereas ErrorDocument is a display handler for http status generated from Apache.

不幸的是,这对于常用方法是不可能的,因为 php 将标头直接发送到用户的浏览器(而不是 Apache Web 服务器),而 ErrorDocument 是从 Apache 生成的 http 状态的显示处理程序。