模拟 403 错误页面

发布于 2024-10-18 11:18:32 字数 191 浏览 8 评论 0原文

我知道您可以发送一个标头,告诉浏览器此页面被禁止,例如:

header('HTTP/1.0 403 Forbidden');

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

默认情况下,仅发送标头会显示一个白色页面,但我记得不久前读过您可以使用客户错误页面。有人知道吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

蓝天 2024-10-25 11:18:32

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

header('HTTP/1.0 403 Forbidden');

echo 'You are forbidden!';

禁止

Just echo your content after sending the header.

header('HTTP/1.0 403 Forbidden');

echo 'You are forbidden!';

forbidden

暖心男生 2024-10-25 11:18:32

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

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

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

http_response_code(403);
die('Forbidden');
和影子一齐双人舞 2024-10-25 11:18:32

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

Include the custom error page after changing the header.

濫情▎り 2024-10-25 11:18:32

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

header("HTTP/1.1 403 Forbidden" );

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

exit;

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

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

ErrorDocument 403 /error403.php

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

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

ErrorDocument 403 /error403.php
慈悲佛祖 2024-10-25 11:18:32

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

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

如果您

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

使用 Apache 或任何其他服务器配置的 HTTP 403 的正常行为进行调用,则会遵循此操作。

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

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.

还给你自由 2024-10-25 11:18:32

我已经阅读了这里的所有答案,但没有一个是适合我的情况的完整答案(这与这个问题完全相同),所以这是我如何收集建议答案的一些部分并提出确切的解决方案

  1. :服务器真实的403页面。 (转到服务器上禁止的 URL,或转到您喜欢的任何 403 页面)
  2. 右键单击并选择“查看源代码”。选择所有源并将其保存到您的域中的文件中,例如:http://domain.com/403.html
  3. 现在转到您真正的禁止页面(或 php 某些部分的禁止情况)示例: http: //domain.com/members/this_is_forbidden.php
  4. 在任何 HTML 输出或标头之前下面回显此代码! (即使是空格也会导致 PHP 发送 HTML/TEXT HTTP 标头并且不起作用)
    下面的代码应该是您的第一行

     

现在您已经有了确切的解决方案。我向 CPANEL 最新访客进行了检查和验证,它被注册为确切的 403 事件。

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 before any 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);
    

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

农村范ル 2024-10-25 11:18:32

.htaccess

ErrorDocument 403     /403.html

.htaccess

ErrorDocument 403     /403.html
琉璃梦幻 2024-10-25 11:18:32

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

.htaccess

ErrorDocument 403 "Forbidden"

PHP

header('HTTP/1.0 403 Forbidden');

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

To minimize the duty of the server make it simple:

.htaccess

ErrorDocument 403 "Forbidden"

PHP

header('HTTP/1.0 403 Forbidden');

die(); // or your message: die('Forbidden');
一身骄傲 2024-10-25 11:18:32

使用 ModRewrite:

RewriteRule ^403.html$ - [F]

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

Use ModRewrite:

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.

世俗缘 2024-10-25 11:18:32

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

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

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.

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.

不语却知心 2024-10-25 11:18:32

发送403后刷新页面:

<?php 
header('HTTP/1.0 403 Forbidden');
?>
<html><head>
<meta http-equiv="refresh" content="0;URL=http://my.error.page">
</head><body></body></html>

Refresh the page after sending the 403:

<?php 
header('HTTP/1.0 403 Forbidden');
?>
<html><head>
<meta http-equiv="refresh" content="0;URL=http://my.error.page">
</head><body></body></html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文