如何使用 PHP 获取 Apache HTTP 响应代码(或服务器响应的第一行) [英] How to get Apache HTTP response code (or first line of server response) with PHP

查看:36
本文介绍了如何使用 PHP 获取 Apache HTTP 响应代码(或服务器响应的第一行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取服务器 HTTP 响应的第一行(在我的例子中是 Apache2)以在我的 PHP 脚本中使用?例如:

How can I get the first line of the servers' HTTP response (Apache2 in my case) for use in my PHP scripts? For example:

HTTP/1.1 200 OK
HTTP/1.1 404 Not Found
Etc.

或者更好的是,只获取我的服务器发送给客户端的 HTTP 响应代码.

Or better yet, get just the HTTP response code that my server sends to the client.

令我惊讶的是,$_SERVER 没有.http_response_code() 似乎只有在以下情况下才能获得响应代码它首先由 PHP 设置.apache_response_headers() 似乎没有.get_headers() 似乎需要额外的步骤/连接(如果我错了,请纠正我).

To my surprise, $_SERVER doesn't have it. http_response_code() seems to only get the response code if it's first been set by PHP. apache_response_headers() doesn't seem to have it. get_headers() seems like it would require an extra step/connection (correct me if I'm wrong).

如果我在 httpd.conf 中设置 ErrorDocument 403/index.php,我可以使用 $_SERVER["REDIRECT_STATUS"] 从 PHP 获取响应代码,但是还有别的方法吗?

If I set ErrorDocument 403 /index.php in httpd.conf, I can use $_SERVER["REDIRECT_STATUS"] to get the response code from PHP, but is there another way?

为了清楚起见,我觉得奇怪的是 $_SERVER 返回几乎所有数据位,除了响应的第一行或响应代码.也许这是 HTTP 协议/服务器技术/PHP 之间的一般限制?也许我不应该尝试在我的 PHP 脚本中访问这些信息?

Just to be clear, I find it odd that $_SERVER returns almost every bit of data except for the first line of the response or the response code. Maybe it's a general limitation between the HTTP protocol/server technology/PHP? Maybe I shouldn't be trying to access this info in my PHP scripts?

推荐答案

更新:不可能 除非您代理自己的请求.将您的请求发送到面向公众的 URL,然后将它们重定向到一个内部变体,您就可以看到该过程中的标头.但这也不是一件容易的事,而且性能也很繁重.或者,您可以编写一个 Http Server 扩展程序,看看它会将您带到何处:)

UPDATE: NO IT'S NOT POSSIBLE Unless you proxy your own requests. Send your request to a Public facing URL and then redirect them to an internal variant and you get to see the headers in the process. But it's not an easy feat and performance taxing also. Or you can write a Http Server extension and see where that takes you :)

示例:

  • 请求来到/page.html
  • 您附加 ?proxy=true 并转发到/page.html?proxy=true
  • 尝试在内部加载新 URL,将原始请求转发给您的内部处理程序
  • 检查所有 $_GET['proxy'] === 'true' 请求是否来自本地 IP
  • 如果你有一个代理 === true 然后用通常的方式处理这个
  • /page.html 从/page.html?proxy=true 获取数据
  • 日志标题
  • 将数据转发回原始客户端
  • (不是很容易,如果做错了会影响性能)

第一次尝试回答

http://php.net/manual/en/function.headers-list.php - 可能是这个吗?

但这适用于您将要发送的标题...

But this works for the headers you are about to send...

register_shutdown_function(function(){
    // Headers are really sent here, do some comparison
    var_dump(headers_list());
});

.htaccess 变体,将所有丢失的文件定向到 index.php.

.htaccess variant that directs all missing files to index.php.

RewriteEngine on
#If handler file is missing, pull index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L,NC]

试试这个.

这篇关于如何使用 PHP 获取 Apache HTTP 响应代码(或服务器响应的第一行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆