简体   繁体   English

输出后更改HTTP标头

[英]changing http headers after output

I have a few questions regarding headers and output buffering. 我有一些关于标头和输出缓冲的问题。

I know headers must be send before output or they will not work, and that output buffering stores all HTML into a buffer and sends it as one as opposed to PHP processes sent bits at a time. 我知道标题必须在输出之前发送,否则它们将不起作用,并且输出缓冲将所有HTML存储到一个缓冲区中,并将其作为一个发送,这与PHP进程一次发送的位不同。

So does this mean when output buffering is on, all content is collected into one variable and where ever the headers were defined in the script they will be placed at the top/first? 那么,这是否意味着在打开输出缓冲时,所有内容都被收集到一个变量中,并且在脚本中定义了标头的任何位置,标头都将放在顶部/顶部?

And if output buffering is off you have to declare headers before any output? 如果关闭输出缓冲,则必须在任何输出之前声明标头?

And also to use any output buffering functions such as ob_clean() you need output buffering to be on? 还要使用任何输出缓冲功能(例如ob_clean()是否需要打开输出缓冲? as if output buffering was off, you couldn't clean, 'take back', anything as it would of already been sent? 好像输出缓冲已关闭,您无法清理,“收回”任何东西,就像已经发送了一样?

Finally is output buffering turned on/off within php.ini? 最后是在php.ini中打开/关闭输出缓冲吗? as my XAMPP local host server seems to have output buffering on and my VPS doesn't, meaning I need to go to my VPS php.ini? 因为我的XAMPP本地主机服务器似乎已打开输出缓冲,而我的VPS没有,这意味着我需要转到我的VPS php.ini?

So does this mean when output buffering is on, all content is collected into 
one variable and where ever the headers were defined in the script they will be 
placed at the top/first?

Not quite, headers are sent regardless of buffering state, all other echo & print are buffered until you release them. 不完全是,无论缓冲状态如何,头都会被发送,所有其他echo & print被缓冲,直到释放它们为止。

And if output buffering is off you have to declare headers before any output?

Yes this is correct. 是的,这是正确的。

The Automatic state of Output Buffering is controlled from the PHP.INI 输出缓冲的自动状态由PHP.INI控制

The manual 手册

Assuming your XAMMP is your development environment I suggest changing that to match your LIVE VPS environment. 假设您的XAMMP是您的开发环境,建议您更改它以使其与LIVE VPS环境匹配。

So does this mean when output buffering is on, all content is collected into one variable and where ever the headers were defined in the script they will be placed at the top/first? 那么,这是否意味着在打开输出缓冲时,所有内容都被收集到一个变量中,并且在脚本中定义了标头的任何位置,标头都将放在顶部/顶部?

Most like that. 最喜欢那样。 There's no "PHP variable" that contains the content but you can access it using the ob_*() functions. 没有包含内容的“ PHP变量”,但是您可以使用ob _ *()函数访问它。 For instance,ob_get_contents() to get the buffered content, ob_clean() to erase it, and so on. 例如,ob_get_contents()获取缓冲的内容,ob_clean()擦除其内容,依此类推。

Headers are sent just before the first content is sent. 标头是在发送第一个内容之前发送的。 In fact you can override previously specified headers using the replace (the second argument) of the header() function. 实际上,您可以使用header()函数的replace(第二个参数)覆盖以前指定的标题。

And if output buffering is off you have to declare headers before any output? 如果关闭输出缓冲,则必须在任何输出之前声明标头?

Yes, otherwise you get a "Warning: Cannot modify header information - headers already sent...". 是的,否则您将收到“警告:无法修改标题信息-标题已发送...”。

And also to use any output buffering functions such as ob_clean() you need output buffering to be on? 还要使用任何输出缓冲功能(例如ob_clean()),是否需要打开输出缓冲?

No, but you need to call ob_start() to start the buffering process. 否,但是您需要调用ob_start()以启动缓冲过程。

Finally is output buffering turned on/off within php.ini? 最后是在php.ini中打开/关闭输出缓冲吗?

Check the output_buffering directives. 检查output_buffering指令。

meaning I need to go to my VPS php.ini? 这意味着我需要转到我的VPS php.ini?

The output buffering feature is configurable PHP_INI_PERDIR. 输出缓冲功能是可配置的PHP_INI_PERDIR。 You can use a .htaccess file or you can just add ob_start() at the beginning of your script. 您可以使用.htaccess文件,也可以只在脚本的开头添加ob_start()。

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

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