开发者

How do headers work with output buffering in PHP?

开发者 https://www.devze.com 2023-01-05 02:35 出处:网络
Title is self-explanatory. I have a goo开发者_开发知识库d bit of experience with PHP, but I am not sure how the header function works between ob_start() and ob_end_clean().

Title is self-explanatory.

I have a goo开发者_开发知识库d bit of experience with PHP, but I am not sure how the header function works between ob_start() and ob_end_clean().

Consider this:

ob_start();

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

$output = ob_get_contents();
ob_end_clean();

echo $output;

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?


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号