How to redirect with header location in php when using ob_start?

CRISHK Corporation picture CRISHK Corporation · Aug 6, 2012 · Viewed 9.8k times · Source
<?php

ob_start();

echo "<body><p>Hello "

if ($condition) {
   header( "Location: http://www.google.com/" );
   exit;
}

echo " World!</p></body>";
ob_end_flush();

?>

When $condition is true I get this:

<body>Hello

What I want is when $condition will be true then go to Google!!!

I don't know what is happening, can you explain or give me a solution!?

Thanks.

Answer

deviousdodo picture deviousdodo · Aug 6, 2012

Just add ob_end_clean(); before the header call.