且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

参考 - 这个错误在PHP中是什么意思?

更新时间:2023-01-24 12:49:51

警告:无法修改标题信息 - 已发送的标题



您的脚本尝试向客户端发送一个HTTP头,但是之前已经输出了,这导致头文件已经发送给客户端。

Warning: Cannot modify header information - headers already sent

Happens when your script tries to send a HTTP header to the client but there already was output before, which resulted in headers to be already sent to the client.

这是一个 E_WARNING ,它将不要停止脚本。

This is an E_WARNING and it will not stop the script.

一个典型的例子是这样的模板文件:

A typical example would be a template file like this:

<html>
    <?php session_start(); ?>
    <head><title>My Page</title>
</html>
...

session_start()函数将尝试使用会话cookie向客户端发送头文件。但PHP在向输出流写入< html> 元素时已经发送了标题。您必须将 session_start()移动到顶部。

The session_start() function will try to send headers with the session cookie to the client. But PHP already sent headers when it wrote the <html> element to the output stream. You'd have to move the session_start() to the top.

您可以通过浏览之前的行代码触发警告并检查其输出位置。在该代码之前移动任何标题发送代码。

You can solve this by going through the lines before the code triggering the Warning and check where it outputs. Move any header sending code before that code.

经过忽略的输出是PHP关闭后的新行?$ c>?> 。当它是文件中的最后一件事情时,它被认为是省略?> 的标准做法。同样,此警告的另一个常见原因是当开始<?php 之前有一个空的空格,行或不可见的字符,导致网络服务器发送头文件因此,当PHP开始解析时,空格/换行符将无法提交任何标题。

An often overlooked output is new lines after PHP's closing ?>. It is considered a standard practice to omit ?> when it is the last thing in the file. Likewise, another common cause for this warning is when the opening <?php has an empty space, line, or invisible character before it, causing the webserver to send the headers and the whitespace/newline thus when PHP starts parsing won't be able to submit any header.

如果您的文件有多个<? php??> 代码块,它们之间不应有任何空格。 (注意:如果您有自动构建的代码,则可能有多个块)

If your file has more than one <?php ... ?> code block in it, you should not have any spaces in between them. (Note: You might have multiple blocks if you had code that was automatically constructed)

还要确保您的代码中没有任何字节顺序标记,例如当脚本的编码是具有BOM的UTF-8时。

Also make sure you don't have any Byte Order Marks in your code, for example when the encoding of the script is UTF-8 with BOM.

相关问题:

  • Headers already sent by PHP
  • All PHP "Headers already sent" Questions on ***
  • Byte Order Mark
  • What PHP Functions Create Output?