P粉4974634732023-08-21 11:59:37
When your script attempts to send HTTP headers to the client, but there is already output before, this causes 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, as shown below:
<html> <?php session_start(); ?> <head><title>我的页面</title> </html> ...
session_start()
The function will try to send header information with the session cookie to the client. But when PHP writes the <html>
element to the output stream, the header information has already been sent. You need to move session_start()
to the front.
You can solve this problem by looking at the line before the code that triggered the warning, and check where the output is. Move any code that sends header information before this code.
An often overlooked piece of output is the newline character after PHP's closing tag ?>
. Common practice is to omit ?>
on the last line of the file. Again, another common cause of this warning is a space, newline, or invisible character before the opening <?php
, causing the web server to send header information and the whitespace/newline character, so when PHP cannot submit any header information when it starts 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)
Also make sure you don't have any byte order marks in your code, e.g. the script is encoded as UTF-8 with BOM.
Related questions: