Avoiding: Headers already sent or Cannot modify header information

David Carr

1 min read - 6th May, 2011

From time to time you may want to refresh the page or redirect the page with php this is very easy to do using the php header command, To use a header it has to be used before any output has started on the page. In other words it has to come before anything is printed including the docytype.

This is less then ideal, thankfully there is a way to use the header function after output has starting by turning on output buffering this enabled you to call a header function anywhere on the page, the syntax to use is very simply at the very top of the file open php then then type ob_start() this turns out output buffering:

<?php ob_start(); ?>

Now you can call a header function anywhere within your page. To make sure you clear resources that are not being used at the very bottom of your file flash the buffer bu typing ob_flush() inside php:

<?php ob_flush(); ?>

This is a very simple but powerful solution.

0 comments
Add a comment

Copyright © 2024 DC Blog - All rights reserved.