Skip to main navigation
#! code
Code Tips, Snippets, Resources, Tutorials And Help
  • Home
  • Tools
  • About
  • Contact

PHP Headers Already Sent Error

By philipnorton42 on 14th February 2008

Try running the following PHP script.

 

You will either see normal output or get the following error messages.

  1. Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at test.php:2) in test.php on line 3
  2.  
  3. Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at test.php:2) in test.php on line 3

This is because when you try to start the session it adds items to the headers outputted by the browser, including the setting up of cookies. To stop this happening you need to ensure that the session_start() function call is put before any output from the browser. This is the case for all header modifying functions including set_cookie() and header();

 

These error messages can also be seen if you add any white space to your files before and after the  and tags. To get around this you can use the trim() function on all of the files that you include. The following code will run through a directory and trim all of the white space from all of the files after backing the files up in case anything goes wrong.

  1. <?php $d = dir('folder') or die('Can\'t find directory');
  2. while (false !== ($f = $d-?>read())) {
  3. $file = $d->path.'/'.$f;
  4. if (is_file($file)) {
  5. copy($file, $file.'.bak');
  6. $contents = trim(join('',file($file)));
  7. $fh = fopen($file,'w') or die('Can\'t open file: '.$file);
  8. if (-1 == fwrite($fh,$contents)) {
  9. die('Can\'t write to file: '.$file);
  10. }
  11. fclose($fh) or die('Can\'t close file: '.$file);
  12. }
  13. }
  14. ?>

If you don't see the above error messages then your PHP engine has been set up with output buffering on. Open up the php.ini file and look for the following line.

output_buffering = 4096

Change it to this and restart the server.

output_buffering = off

Output buffering works by stopping all output to the browser until PHP has run through the code, at least up until the first 4096 bytes (the default option set in the php.ini file). It will then run any header scripts and the send any output to the browser. The drawback of using this method is that it can appear to slow down the server while the preprocessing is taking place as no output is displayed to screen until the last possible moment.

Category
PHP
Tags
PHP

Add new comment

The content of this field is kept private and will not be shown publicly.
About text formats
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Categories

  • Ansible
  • Apache
  • Book Reviews
  • CSS
  • DOS/Windows
  • Drupal
    • Drupal 8
    • SimpleTest
  • Flex/Flash
  • General
  • Git
  • HTML/XHTML
  • JavaScript
    • JavaScript Strings
    • JavaScript Websites
    • JQuery
    • MooTools
    • OpenLayers
    • Script.aculo.us
  • Linux/Unix
  • MS SQL
  • OSX
  • PhoneGap
  • PHP
    • Phing
    • PHP Arrays
    • PHP Questions
    • PHP Strings
    • PHP Websites
    • Zend Framework
  • PostgreSQL
  • Python
  • Regular Expressions
  • Regular Expressions Websites
  • SQL
    • MS SQL
    • MySQL
    • PostgreSQL
  • Vagrant
  • Websites
  • WordPress

User login

  • Reset your password

© 2019 #! code

  • Twitter
  • Facebook
  • Google+
  • Github
  • RSS
  • Colophon
  • Privacy Policy
  • Terms and License