PHP 警告:标头已以未知方式发送

发布于 2024-07-15 03:01:33 字数 112 浏览 4 评论 0原文

我正在寻找可能触发以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

辞别 2024-07-22 03:01:33

原来是这条线

ob_start("ob_gzhandler");

引起了警告。 这似乎已于 2001 年报告并修复,但出于某种原因它不断回来。

Turned out that it was the line

ob_start("ob_gzhandler");

that caused the warning. This has been reported and fixed in 2001, it seems, but for some reason it keeps coming back.

高速公鹿 2024-07-22 03:01:33

它可能有很多东西,但正如其他人所说,它通常只是输出某个地方的一个空间,然后是 header() 命令被发送,通常情况下没问题,但在开始发送回内容后就不行了(在这种情况下可能只是一个空格)。

使用 ob_start() 通过缓冲来阻止输出立即出去。 所以这是一个潜在的解决方案,或者至少是诊断它来自何处的方法 zodeus 说道


导致这些空间丢失的一个常见原因是在这种情况下。

global.php

<?php
  $variable = 1;
  $database = 'something else';
?> <-- A space here
 <-- Or here

index.php

<?php

  require('global.php');
  $var = dosomething();
  header('Location: http://www.example.com/');

?>

解决这个问题的一种方法是删除 ?>; 在 global.php 文件的末尾。 您不需要这些,只有当您开始在 PHP 代码后面添加 HTML 时,它们才有用。 所以你会:

<?php
  $variable = 1;
  $database = 'something else';

当你执行 require() 时,在 header() 之前不会输出空格。


只是为了说明输出内容和标题的问题,其他常见情况也会出现类似的错误。 当您在使用 header() 重定向后忘记停止处理时,就会发生这种情况。

if ($notLoggedIn) {
  header('Location: http://www.example.com/login.php');
}
echo 'Welcome to my website'; // this will be outputted, 
                              // you should have an exit() 
                              // right after the 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

<?php
  $variable = 1;
  $database = 'something else';
?> <-- A space here
 <-- Or here

index.php

<?php

  require('global.php');
  $var = dosomething();
  header('Location: http://www.example.com/');

?>

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:

<?php
  $variable = 1;
  $database = 'something else';

And when you do the require(), the space is not outputted before the header().


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

if ($notLoggedIn) {
  header('Location: http://www.example.com/login.php');
}
echo 'Welcome to my website'; // this will be outputted, 
                              // you should have an exit() 
                              // right after the header()
深陷 2024-07-22 03:01:33

我认为发生的事情是内置的 php 函数之一正在输出一些东西。 我在几个 IMAP 函数中看到过这种情况,它们只输出一个“”(空格字符),结果就把事情搞砸了。
您可以尝试使用 Xdebug 或 Zend 调试器来跟踪它,但如果您两者都没有
尝试将输出缓冲包装在您认为可能导致此问题的某些函数周围。

ob_start();
callYourFunction();
ob_end_clean();

一次执行此一个函数,当错误消失时,您就会知道哪个函数导致了您的问题,然后您可以提交错误报告或将其作为黑客保留。 但至少你知道是什么功能导致了问题。

编辑:事实上,您的输出发生在第 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.

ob_start();
callYourFunction();
ob_end_clean();

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.

情未る 2024-07-22 03:01:33

您是否检查过文件中是否存在意外的 UTF-8 BOM

Have you checked your files for unintended UTF-8 BOMs?

雨的味道风的声音 2024-07-22 03:01:33

该错误告诉您某些内容已发送输出,这将强制发送标头,因为标头必须写在 http 消息正文之前。

我发现的最常见问题是标题中的文本。 vis:

<?php // myfile.php
  include 'header.php';
?>

在 header.php 中:

<?php // 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:

<?php // myfile.php
  include 'header.php';
?>

and in header.php:

<?php // 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 '?>'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文