Jump to content

HEADER ERRORS - READ HERE BEFORE POSTING THEM


akitchin

Recommended Posts

i've seen this error pop up time and time again, and the answer is ALWAYS the same.  please look here before you post it.  the problem is you are outputting to the browser (whitespace included) before sending a header.  this is unallowed.  remove output prior to the header, or use OUTPUT BUFFERING.

 

NEW EDIT TO THIS TOPIC:

 

what usually results in these errors is poor script design.  when one processes a form, they should do it BEFORE OUTPUTTING ANYTHING TO THE BROWSER.  there is NO reason that the process cannot be operated in the header of the document, before any HTML is output.

 

let's take a common example.  the programmer wants a login form which checks the username and password against the database.  if they don't match, then don't set a cookie remembering them and tell them it failed.  if they do match, set the cookie and send them to a member page.  many will do:

 

[php processing the form]

[form code]

 

because they can simply echo any errors straight from PHP right above that form.  they can also send a success message, set the cookie, and header() them off on their merry way without seeing the form again.  it's a logical place to put this code.  think again.  you'll (perhaps not so) obviously get header errors when you go to use setcookie() and header().  if the login fails, it's dandy, but if you have a successful login the user gets nowhere.

 

answer:  put the processing in the header, and store the results in variables.  perhaps a $result variable that is 1 if successful, 0 if failed.  then $output that contains either a success message or customized error messages.  the new code would look like:

 

[php processing the form (if it was sent) and storing the results]

[html starting the page and layout]

[php echoing the results]

[form code if failed - exit(); if successful]

 

this will solve your header errors and will make for much more maintainable and readable code.  if you want to redirect them elegantly (with a success message), use a <meta> refresh redirect.  header() should only be used in instances where instant redirection is desired.

Link to comment
Share on other sites

  • 5 weeks later...

Sometimes I see members posts where they have their logic to where it outputs stuff on purpose, then they try and use setcookie, session_start() or header with location commands. These types of commands need to be placed (preferably towards the top) prior to any HTML or output sent in your script. So, do not send ANYTHING to the browser prior to using these commands.

 

If you absolutely need to send/output something first, then use ob_start() at the top of your script (before anything else) to initiate output buffering. This will stop the “headers already sent” warning messages, but this should be looked at like a band-aid and not a permanent solution.

 

The main solution is to change your logic and/or locate in your code where you’re outputting something unintentionally before the execution of these commands. This could be HTML/JavaScript, a space, a tab, a newline, hidden character(s), etc. etc.

 

Some text editors could put hidden characters or markers causing output to be sent to the browser without you being initial aware of this.

 

The following information should help you resolve your "headers already sent" warning messages:

 

Check that there's no characters (output) before a starting PHP tag <?PHP

Check that there's no characters (output) after an ending PHP tag ?>

Check the above in any include/require files you're using in your scripts, and of course make sure they're not echoing something first

 

Some people just want to not display these "headers already sent" warnings by changing error_reporting(), but not showing them does not really solve the underlying problem and your script will probably not work as you intended because the command that invoked the warning did not complete right (like not continuing with existing session or not saving the session in the case of session_start() called).

 

 

You can search these forums too because there’s lots of them out there. It’s a very popular problem.

 

Link to comment
Share on other sites

  • 5 years later...
Documents encoded in UTF-8 are becoming more and more common. Many programs, however, will add a [url=http://en.wikipedia.org/wiki/UTF-8#Byte_order_mark]byte order mark[/url] at the very beginning of UTF-8 encoded documents. This appears invisible in the editor and is actually placed before any text you write. This can be a problem when working with PHP scripts. PHP treats the byte order mark as text that needs to be output, and this will happen before it starts executing any PHP code, even if the PHP code appears at the very beginning of the document when viewing it in an editor. Now, the issue here is that you don't get a chance to start output buffering (ob_start()) or call any functions that manipulate the HTTP response headers (session_start(), setcookie(), header() and so on) before the output is started.

All of this can be avoided if you make sure that your favorite editor saves your documents without the byte order mark or, alternatively, by enabling output buffering in php.ini.
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.