Current Location: Home> Latest Articles> Best Practices for Using the headers_sent() and header() Functions Together

Best Practices for Using the headers_sent() and header() Functions Together

gitbox 2025-08-19
<?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.

2. Functionality of headers_sent()

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.

3. Best Practices for Combined Use

  • 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.

4. Sample Code

<span><span><span class="hljs-meta">&lt;?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">"&lt;script&gt;window.location.href='http://example.com';&lt;/script&gt;"</span></span><span>;
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"&lt;p&gt;Redirect failed, please click &lt;a href='http://example.com'&gt;here&lt;/a&gt;.&lt;/p&gt;"</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">?&gt;</span></span><span>
</span></span>

5. Conclusion

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>