KNOWNHOST WIKI

User Tools

Site Tools


developmental:what-is-the-headers-already-sent-php-error

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
developmental:what-is-the-headers-already-sent-php-error [2016/10/26 10:52]
mscherf [Possible solution] added link
developmental:what-is-the-headers-already-sent-php-error [2020/06/10 09:26] (current)
Karson N.
Line 2: Line 2:
 A common error seen, however rarely known or understood is the "headers already sent" error. Which may look similar to this: A common error seen, however rarely known or understood is the "headers already sent" error. Which may look similar to this:
  
-<code>Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23</code>+<code> 
 +  Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23 
 +</code>
  
 +\\
 ===== Why does it happen? ===== ===== Why does it happen? =====
 To understand why headers must be sent before output it's necessary to look at a typical HTTP response. PHP scripts mainly generate HTML content, but also pass a set of HTTP/CGI headers to the webserver: To understand why headers must be sent before output it's necessary to look at a typical HTTP response. PHP scripts mainly generate HTML content, but also pass a set of HTTP/CGI headers to the webserver:
  
-<code>HTTP/1.1200 OK +<code> 
- Powered-By: PHP/5.3.7Vary:Accept-EncodingContent-Type: text/html; charset=utf-8<html><head><title>PHP page output page</title></head><body><h1>Content</h1> <p>Some more output follows...</p>and<a href="/"><img src=about:note>...</code>+  HTTP/1.1200 OK 
 +  Powered-By: PHP/5.3.7Vary:Accept-EncodingContent-Type: text/html; charset=utf-8<html><head><title>PHP page output page</title></head><body><h1>Content</h1> <p>Some more output follows...</p>and<a href="/"><img src=about:note>... 
 +</code>
  
 The page/output always follows the headers. PHP is required to pass the headers to the webserver first. It can only do that once. And after the double linebreak it can't ever append to them again. The page/output always follows the headers. PHP is required to pass the headers to the webserver first. It can only do that once. And after the double linebreak it can't ever append to them again.
Line 14: Line 19:
 When PHP receives the first output (''print'', ''echo'', ''<html>'') it will "flush" the collected headers. Afterwards it can send all the output bits it wants. But sending further headers is impossible from then. When PHP receives the first output (''print'', ''echo'', ''<html>'') it will "flush" the collected headers. Afterwards it can send all the output bits it wants. But sending further headers is impossible from then.
  
 +\\
 ===== Functions that modify the HTTP header ===== ===== Functions that modify the HTTP header =====
   * header   * header
Line 23: Line 29:
  
  
 +\\
 ===== Possible solution ===== ===== Possible solution =====
 <WRAP center round info 100%> <WRAP center round info 100%>
Line 49: Line 56:
   - DO NOT PUT CODE IN UNNECESSARY CODE BLOCKS, PUT THEM IN A SINGLE PHP BLOCK.   - DO NOT PUT CODE IN UNNECESSARY CODE BLOCKS, PUT THEM IN A SINGLE PHP BLOCK.
  
 +\\
 ===== Referenced and helpful resources ===== ===== Referenced and helpful resources =====
   * [[http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php]]   * [[http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php]]
   * [[http://php.net/manual/en/function.headers-sent.php]]   * [[http://php.net/manual/en/function.headers-sent.php]]
   * [[http://codex.wordpress.org/FAQ_Troubleshooting#How_do_I_solve_the_Headers_already_sent_warning_problem.3F]]   * [[http://codex.wordpress.org/FAQ_Troubleshooting#How_do_I_solve_the_Headers_already_sent_warning_problem.3F]]
developmental/what-is-the-headers-already-sent-php-error.1477497156.txt.gz · Last modified: 2016/10/26 10:52 by mscherf