P粉6548949522023-08-22 13:13:35
When your script attempts to send HTTP headers to the client, but there is already output before, causing the headers to have already been sent to the client.
This is an E_WARNING
, which does not stop the execution of the script.
A typical example is a template file like this:
<html> <?php session_start(); ?> <head><title>My Page</title> </html> ...
session_start()
The function will try to send a header with the session cookie to the client. But when PHP writes the <html>
element to the output stream, the header has already been sent. You need to move session_start()
to the top.
You can resolve this issue by examining the line of code before the code that triggered the warning, and seeing where the output is. Move any code that sends headers before this code.
An often overlooked piece of output is the newline character after the PHP closing tag ?>
. It is generally considered standard practice to omit ?>
when it is the last item in the file. Again, another common cause of this warning is that there is a space, newline, or invisible character before the opening <?php
, causing the web server to send the header and the whitespace/newline character, so when PHP No headers could be submitted when starting parsing.
If you have multiple <?php ... ?>
code blocks in your file, make sure there aren't any spaces between them. (Note: If you have auto-generated code, there may be multiple code blocks)
Please also make sure there are no Byte Order Marks in your code, for example the script is encoded as UTF-8 with BOM.
Related questions: