Black Friday sale is now on! 50% off Laravel: The Modular Way. Learn more

Avoiding: Headers already sent or Cannot modify header information

David Carr

PHP & MySQL Tutorials

    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.

    Laravel Modules Book by David Carr

    Help support the blog so that I can continue creating new content!

    Subscribe to my newsletter

    Subscribe and get my books and product announcements.

    © 2009 - 2022 DC Blog. All code MIT license. All rights reserved.