基本上,我正在写一个wordpress插件。我的插件所做的第一个功能是确定计划发送哪些HTTP响应代码和标头(在发送之前)。
根据http状态代码,它会放置一个钩子,并可能根据其他事情修改HTTP响应。
这是我写的第一个插件,您可以猜测我的经验 - 我仍然使用插件编写的基础知识。
答案 0 :(得分:2)
headers_list
函数将为您提供已发送或即将发送的所有标头的列表。您可以确定他们是否已使用headers_sent
功能发送,但我认为您无法保证在发送之前拦截它们。我可能错了 - 虽然可能有输出缓冲,但我从未研究过。
答案 1 :(得分:2)
你必须组合使用headers_sent()和headers_list(),检查标题是否与第一个一起发送,并迭代第二个提供的结果并根据你的逻辑更改它们
答案 2 :(得分:2)
请参阅headers_list()
和http_response_code()
。
请注意,http_response_code()
是新的,可能在您的PHP版本中不可用。没有其他方法可以检测到它。抱歉。但如果您没有自己更改,状态代码应始终为“200 OK”。
/* get headers and HTTP status code (which should be always 200,
if you didn't change it before calling this code) */
if (!headers_sent()) {
$headers = headers_list();
$status = http_response_code();
// change if necessary
}
else {
// oh noes, cannot change headers anymore
}