Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Avoiding: Headers already sent or Cannot modify header information

David Carr

Tutorials PHP & MySQL

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 Your Logo Your Logo Your Logo

Become a sponsor

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

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.