模拟 403 错误页面
我知道您可以发送一个标头,告诉浏览器此页面被禁止,例如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
只需在发送标头后回显您的内容即可。
Just echo your content after sending the header.
http_response_code 是在 PHP 5.4 中引入的,使事情变得更加容易!
http_response_code was introduced in PHP 5.4 and made the things a lot easier!
更改标题后包含自定义错误页面。
Include the custom error page after changing the header.
为此,您必须首先向浏览器表明用户收到错误 403。为此,您可以使用以下代码:
然后,脚本发送“错误,错误,错误,错误,错误......”,因此你必须阻止它。您可以使用
这两行,服务器发送错误并停止脚本。
不要忘记:模拟错误,但您必须将其设置在 .htaccess 文件中,并使用
For this you must first say for the browser that the user receive an error 403. For this you can use this code:
Then, the script send "error, error, error, error, error.......", so you must stop it. You can use
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
看到了很多答案,但正确的答案是根据 php 手册提供标头函数调用的完整选项,
如果您
使用 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
If you invoke with
the normal behavior of HTTP 403 as configured with Apache or any other server would follow.
我已经阅读了这里的所有答案,但没有一个是适合我的情况的完整答案(这与这个问题完全相同),所以这是我如何收集建议答案的一些部分并提出确切的解决方案
在任何 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:
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!
Now you have the exact solution. I checked and verified with CPANEL Latest Visitors and it is registered as exact 403 event.
.htaccess
.htaccess
为了最小化服务器的职责,让它变得简单:
.htaccess
PHP
To minimize the duty of the server make it simple:
.htaccess
PHP
使用 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.
我知道您有一个在 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.
发送403后刷新页面:
Refresh the page after sending the 403: