简体   繁体   English

发送http-404响应但不发送IIS服务器中设置的自定义错误页面 PHP

[英]Send http-404 response BUT dont send custom error page setted in IIS server | PHP

Lets understand the scenario: 让我们了解场景:

:- Using PHP for a web application on IIS 8.5 :-在IIS 8.5上将PHP用于Web应用程序

:- A custom error page is set for 404 with following web-config snippt: :-使用以下Web配置片段为404设置了一个自定义错误页面:

<httpErrors errorMode="Custom" existingResponse="Replace">
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" path="/path/404.php" responseMode="ExecuteURL" />
</httpErrors>

:- This is working for non-existing page and IIS also return this 404.php if I set header to 404 (via header('HTTP/1.0 404 Not Found', true, 404); die; ) in any PHP page. :-这适用于不存在的页面如果我在任何PHP页面中将header设置为404(通过header('HTTP/1.0 404 Not Found', true, 404); die; ),则IIS也会返回此404.php。

Question: 题:

Now, I wan to send 404 http response code in some php-file on any specific condition, lets say if database returns 0 rows. 现在,我希望在任何特定条件下在某些php文件中发送404 http响应代码,可以说数据库是否返回0行。 I can do that by setting header as above but that will return the custom error page also. 我可以通过如上所述设置标头来做到这一点,但这也会返回自定义错误页面。 Is there any way to send 404 code and tell IIS to send response as it is, without sending custom error page along with it? 有什么方法可以发送404代码并告诉IIS照原样发送响应,而无需发送自定义错误页面?

Issues: 问题:

:- If I remove custom error page and put the content between header() and die() then it will meet my requirement but IIS will fail to serve custom error on pages which actually doesn't exist. :-如果删除自定义错误页面并将内容放在header()die()则它将满足我的要求,但是IIS将无法在实际上不存在的页面上提供自定义错误。

:- I am using URL rewrite so that cannot use multiple custom error pages based on location tag and don't want to redirect to any suggested different URLs. :-我正在使用URL重写,因此不能使用基于location标记的多个自定义错误页面,并且不想重定向到任何建议的其他URL。

As I have one tricky solution/idea, I want to share that also. 因为我有一个棘手的解决方案/想法,所以我也想分享一下。 But if anybody have any better solution, I welcome and thanks to them. 但是,如果有人有更好的解决方案,我欢迎并感谢他们。

Trick/Solution/Idea: 招/解决方案/理念:

Step 1:- From any existing php-page set a session variable just before header('HTTP/1.0 404 Not Found', true, 404); 步骤1:-从任何现有的php-page设置一个在header('HTTP/1.0 404 Not Found', true, 404);之前的session variable header('HTTP/1.0 404 Not Found', true, 404); and put a specific message in that variable(I suggest to use different integer code as session variable's value for different php-pages) THEN set header and call die(); or exit; 并在该变量中放置一条特定的消息(我建议使用不同的整数代码作为不同php页面的会话变量的值)然后设置标头并调用die(); or exit; die(); or exit; .

Step 2:- After that IIS or any other server will execute custom error page. 步骤2:-之后,IIS或任何其他服务器将执行自定义错误页面。 SO, in custom error page check the session variable and echo content as follows.... 因此,在自定义错误页面中,检查会话变量并按如下所示回显内容。

........2a - If session variable is not set then display " normal error content with main menu " etc. ........ 2a-如果未设置会话变量,则显示“ 带有主菜单的正常错误内容 ”等。

........2b - If session variable is set then check the value and according to value display the different content you want to show to user like: " data not found message and search panel " ........ 2b-如果设置了会话变量,则检查该值,并根据该值显示要向用户显示的不同内容,例如:“ 未找到数据消息和搜索面板

Note: In custom error page's coding you again need to set 404 header, and ALSO you can send different error code according to session variable's value in above trick. 注意:在自定义错误页面的编码中,您再次需要设置404标头,此外,您还可以根据上述技巧中会话变量的值发送不同的错误代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM