标头如何与PHP中的输出缓冲一起使用? [英] How do headers work with output buffering in PHP?

查看:90
本文介绍了标头如何与PHP中的输出缓冲一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题是不言自明的.

我在PHP方面有丰富的经验,但是我不确定ob_start()ob_end_clean()之间的header函数如何工作.

I have a good bit of experience with PHP, but I am not sure how the header function works between ob_start() and ob_end_clean().

考虑一下:

ob_start();

echo "Some content";
header('X-Example-Header: foo');
echo "Some more content";

$output = ob_get_contents();
ob_end_clean();

echo $output;

header函数是否忽略输出缓冲,因此所有标头都在内容之前发送,因为它是在header调用之后被echo格式化的?

Does the header function ignore the output buffering, and thus all headers get sent before the content because it is echoed after the header call?

还是以其他方式起作用?

Or does it work some other way?

推荐答案

header()确实忽略了输出缓冲.使用输出缓冲的部分原因是因为可以对响应进行缓冲,因此可以无序"发送HTTP标头.发送任何类型的输出后,您将无法发送HTTP标头(除非该输出已缓冲).

The header() does indeed ignore output buffering. Part of the reason to use output buffering is so you can send HTTP headers "out of order" since the response is buffered. You can't send HTTP headers once you've sent any kind of output (unless that output is buffered).

这篇关于标头如何与PHP中的输出缓冲一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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