PHP 警告:标头已以未知方式发送
我正在寻找可能触发以下 PHP 警告的事情:
PHP 警告:无法修改标头 信息 - 标头已发送 第 0 行未知
I'm looking for things that might trigger the following PHP warning:
PHP Warning: Cannot modify header
information - headers already sent in
Unknown on line 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
原来是这条线
引起了警告。 这似乎已于 2001 年报告并修复,但出于某种原因它不断回来。
Turned out that it was the line
that caused the warning. This has been reported and fixed in 2001, it seems, but for some reason it keeps coming back.
它可能有很多东西,但正如其他人所说,它通常只是输出某个地方的一个空间,然后是
header()
命令被发送,通常情况下没问题,但在开始发送回内容后就不行了(在这种情况下可能只是一个空格)。使用
ob_start()
通过缓冲来阻止输出立即出去。 所以这是一个潜在的解决方案,或者至少是诊断它来自何处的方法 zodeus 说道。导致这些空间丢失的一个常见原因是在这种情况下。
global.php
index.php
解决这个问题的一种方法是删除 ?>; 在 global.php 文件的末尾。 您不需要这些,只有当您开始在 PHP 代码后面添加 HTML 时,它们才有用。 所以你会:
当你执行
require()
时,在header()
之前不会输出空格。只是为了说明输出内容和标题的问题,其他常见情况也会出现类似的错误。 当您在使用
header()
重定向后忘记停止处理时,就会发生这种情况。It might be a lot of things, but as the others said, it's often just a space lying around somewhere that gets outputted and then a
header()
command is sent which normally is fine, but not after starting to send content back (potentially just a space in this case).Using
ob_start()
stops the output from going out right away by buffering it. So it's a potential solution, or at least way to diagnose where it's coming from as zodeus said.One common thing that causes those lose spaces are in this scenario.
global.php
index.php
One way to fix that is to remove the ?> at the end of the global.php file. You don't need those, they are only useful if you start putting HTML for example after your PHP code. So you'd have:
And when you do the
require()
, the space is not outputted before theheader()
.Just to illustrate the problems with content outputted and headers is that other common case that gives a similar error. It happens when you forget to stop the processing after a redirection using
header()
.我认为发生的事情是内置的 php 函数之一正在输出一些东西。 我在几个 IMAP 函数中看到过这种情况,它们只输出一个“”(空格字符),结果就把事情搞砸了。
您可以尝试使用 Xdebug 或 Zend 调试器来跟踪它,但如果您两者都没有
尝试将输出缓冲包装在您认为可能导致此问题的某些函数周围。
一次执行此一个函数,当错误消失时,您就会知道哪个函数导致了您的问题,然后您可以提交错误报告或将其作为黑客保留。 但至少你知道是什么功能导致了问题。
编辑:事实上,您的输出发生在第 0 行,这意味着它是一个 C 级函数执行输出,而不是使用 PHP 编写的任何代码。
I think whats happening is one of the built in php functions is outputting something. I've seen this with a couple of the IMAP functions where they out put just a " " (space character) and it screws things up.
You can thry tracking it down using Xdebug or the Zend debugger, but i f you have neither
try wrapping output buffering around some of the functions you think may be cause it.
Do this one function at a time and when the error goes away you'll know which function is cause you the problem, then you can either file a bug report or just leave it in as a hack. But at least then you know what function is cause the issue.
Edit: The fact that is says your output is occurring on line 0 means that it's a C level function doing the output, not any code that's been written using PHP.
您是否检查过文件中是否存在意外的 UTF-8 BOM?
Have you checked your files for unintended UTF-8 BOMs?
该错误告诉您某些内容已发送输出,这将强制发送标头,因为标头必须写在 http 消息正文之前。
我发现的最常见问题是标题中的文本。 vis:
在 header.php 中:
您在这里看不到的是在结束“
?>”之后有一个空格 - 要么是空格,要么是 CR/LF。 这是输出,因为 php 标准规定 php 标记之外的任何内容都会输出为 html。
解决方案是确保删除结束“
?>”之后的所有内容
The error tells you that something has sent output, which would force headers to be sent, because the headers must be written before the body of the http message.
The most common problem I have found is text in headers. vis:
and in header.php:
What you can't see here is that there is a blank - either a space or CR/LF after the closing '
?>
'. This is output because the php standard says that anything outside the php tags is output as html.The solution is to make sure that you make sure to erase everything after the closing '
?>
'