Warning: Cannot modify header information – headers already sent

Headers are sent by server for informing client about some of the important details. Those details can be Content type, encoding, cache used and so on. Generally you do not have to worry about this. Sometime user needs to modify header information. Headers are sent before any output to the client. If you try to send any header information after output is sent then this type of warning is reported by server. This also means that any header you are trying to set will not work.

Two ways to solve this problem "Cannot modify header information - headers already sent":

1. Locate the header()/setcookie() function, which can create this problem.
Check that if it is used before any output is sent to client. Even a single space output before header can cause this header modify warning.
If you could not find any output going before header/setcookie, check for any blank-line/comment before start of <?php tag. Write your PHP start tag (<?php) at the first line of your page. If you left a line inside the PHP start tag (<?php), then it is not a problem, but it should not be before PHP start tag.

2. Use ob_start(); function. If you cannot control the output then use ob_start(); function at top the page, then your problem of "modify header" will be resolved. ob_start() is output controlling function.

Update (May, 2010):
3. Problem can also occurs due to Byte Order Mark (BOM) for UTF-8 encoded pages. Editor do not show (generally) the BOM it inserts at the top of the page. It is inserted even before the PHP's opening tag (<?php). So, output buffering at page level cannot help you here. Now, you have two solutions for this - one is to remove BOM from the top and another is use output buffering from the php.ini setting. Most editors have the setting to remove the BOM at the time of saving the page.

More about headers:
Use of Content Type Header
Header for XML content in PHP file

  • # 1 - by Beth

    this was soooo helpful!! thank you!

  • # 2 - by Beth

    this was soooo helpful!! thank you!

  • # 3 - by P V N

    Very very good information! Thank you very much!!!

  • # 4 - by P V N

    Very very good information! Thank you very much!!!

  • # 5 - by samsami2u

    hey boss, thanks

Comment pages
1 2 3 212
Comments are open for an year period. Please, write here on Facebook page.