Reputation: 13
why is my code not showing/printing "redirecting now ...." text before it redirects.keep in mind that I don't want to use javascript or http_redirect() to redirect.The later will break the flow of my logic.
<?php
ob_start();
echo "redirecting now ....";
sleep(3);
header("Location:index.html");
exit();
ob_end_flush();
?>
Upvotes: 1
Views: 2918
Reputation: 311
you cant output anything before using the header()
HTTP-Headers should be sent before any output from the server. If you have an output before, the server will output a warning like 'Headers already been sent'
Upvotes: 1
Reputation: 3424
Use this instead
<?php
ob_start();
echo "redirecting now ....";
header("Refresh: 3; index.php");
exit();
ob_end_flush();
?>
Upvotes: 3
Reputation: 4161
That won't work, the reason to use ob_start() in that context (IMO) is if you have unavoidable output before the header that's preventing the header() from working.
The reason it won't work is because ob_start() captures all the output (in this case echo "redirecting now....";) and doesn't spit it out until ob_end_flush(). You've redirected the page using header() before the script reaches ob_end_flush().
Upvotes: 1
Reputation: 8179
try this
<?php
echo "redirecting now ....";
print "<META http-equiv='refresh' content='3;URL=index.html'>";
exit;
?>
Upvotes: 2