<?php<br>
// This part of the code is unrelated to the article content, demonstrating a pre-code block<br>
date_default_timezone_set('Asia/Shanghai');<br>
$now = date('Y-m-d H:i:s');<br>
echo "Current time: <span class="hljs-subst">$now\n";<br>
?><span></p>
<p><hr></p>
<p>In PHP development, managing HTTP response headers is a common requirement. The <code></span><span><span class="hljs-title function_ invoke__">header</span></span><span>()
However, if HTTP headers have already been sent (for example, some HTML or whitespace output exists), calling header() will trigger a “Cannot modify header information - headers already sent” error.
headers_sent() checks whether HTTP headers have already been sent to the client. Typical usage:
<span><span><span class="hljs-keyword">if</span></span><span> (!</span><span><span class="hljs-title function_ invoke__">headers_sent</span></span><span>()) {
</span><span><span class="hljs-title function_ invoke__">header</span></span><span>(</span><span><span class="hljs-string">'Location: http://example.com'</span></span><span>);
</span><span><span class="hljs-keyword">exit</span></span><span>;
} </span><span><span class="hljs-keyword">else</span></span><span> {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">'Headers already sent, cannot redirect.'</span></span><span>;
}
</span></span>
This prevents calling header() after headers have already been sent, avoiding runtime errors.
Call header() early and avoid outputting content
Once PHP outputs content, HTTP headers are automatically sent. Ensure no echo, print, or HTML output exists before calling header().
Use headers_sent() to prevent duplicate header sends
In complex programs or template systems, controlling output order can be difficult, making headers_sent() checks crucial.
Provide alternatives when headers are already sent
For example, use JavaScript redirects or manual links to avoid program crashes.
Combine with exit or die
After sending a redirect, immediately exit the script to prevent further code execution.
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Assume some content may have already been output</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Welcome!"</span></span><span>;
</span><span><span class="hljs-keyword">if</span></span><span> (!</span><span><span class="hljs-title function_ invoke__">headers_sent</span></span><span>(</span><span><span class="hljs-variable">$file</span></span><span>, </span><span><span class="hljs-variable">$line</span></span><span>)) {
</span><span><span class="hljs-title function_ invoke__">header</span></span><span>(</span><span><span class="hljs-string">'Location: http://example.com'</span></span><span>);
</span><span><span class="hljs-keyword">exit</span></span><span>;
} </span><span><span class="hljs-keyword">else</span></span><span> {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"<script>window.location.href='http://example.com';</script>"</span></span><span>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"<p>Redirect failed, please click <a href='http://example.com'>here</a>.</p>"</span></span><span>;
</span><span><span class="hljs-title function_ invoke__">error_log</span></span><span>(</span><span><span class="hljs-string">"Headers already sent in <span class="hljs-subst">$file</span> on line <span class="hljs-subst">$line</span>."</span></span><span>);
}
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
Using header() together with headers_sent() significantly improves the safety and flexibility of HTTP header operations. In large projects or frameworks, this check prevents fatal errors caused by output order mistakes, making the code more robust and maintainable. Developing good output control habits and being aware of header checks is an important best practice in PHP development.
<span></span>