DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Desirae has posted 3 posts at DZone. View Full User Profile

Cannot Modify Header Information - Headers Already Sent By FIX

09.09.2006
| 13509 views |
  • submit to reddit
        Buffers output and eliminates problem with spaces/line breaks at the beginning/end of files. Make sure to place ob_start(); before header(); is called.
<?php
ob_start();

header("Location: somepage.php"); //Redirect

ob_end_flush();
exit;
?>
    

Comments

Desirae Beberniss replied on Mon, 2006/08/28 - 1:12am

Yes, this is a quick fix which can be used until you can take the time to hunt down errant spaces or line breaks. It can work in an emergency situation.

Snippets Manager replied on Wed, 2006/08/02 - 11:38am

Thats not a fix, its a hacky patch. If you need to do this, your logic flow in inherently flawed, or your code is mal-formed (in the case of erroneous spaces). Actually fixing it is a better trick.

Logan Koester replied on Sun, 2006/09/10 - 9:05pm

That's a cool trick. I'm not sure if I'll ever have to use it, but thanks :)