4

I have the following dumb code:

<?

echo "<html><br><br><br><div id='loading'><p><img src='loader.gif'> Please wait 6 seconds...</p></div></html>";
sleep(6);
header('Location: http://google.com/');

?>

Why doesn't the HTML piece of code show up in the browser before sleeping 6 seconds and then redirecting? The codes doesn't output the HTML code at all, waits 6 seconds then sends me to the location. What do I do wrong?

2
  • you didnt do anything wrong sleep function is supposed to work like that
    – Nagri
    Sep 6, 2012 at 11:22
  • Won't work! headers would be already committed by the response from echo
    – Vishal
    Sep 6, 2012 at 11:24

4 Answers 4

7

This will not work. Because the first echo will start output. After that header call will just fail. However you can do it with refresh header on PHP side.

<?php
header('Refresh: 5;URL=http://www.google.com/'); // refresh header
echo "<html><head>";
// meta refresh
echo "<meta http-equiv=\"refresh\" content=\"5;URL=http://www.google.com/\" />"; 
echo "</head><body><br><br><br><div id='loading'><p><img src='loader.gif'> Please wait 6 seconds...</p></div></body></html>";
?>
<script type="text/javascript">
// javascript refresh
window.setTimeout(function(){
    location.href="http://www.google.com";
},5000);
</script>

On the above script 3 things were handled.

  1. HTTP Refresh header
  2. Meta refresh
  3. Javascript Refresh

Note: Meta refresh is deprecated (though I have shown in example). HTTP Refresh header is recommended.

0
3

You would need to do something like (this won't work):

<?php

echo "<html><br><br><br><div id='loading'><p><img src='loader.gif'> Please wait 6 seconds...</p></div></html>";
@ob_flush(); //flush the output buffer
flush(); //flush anything else
sleep(6); //wait
header('Location: http://google.com/'); //redirect

?>

However: This won't work as expected, you cannot redirect the browser after sending content (PHP will throw and error and tell you this)

Instead you should:

<?php

echo "<html><meta http-equiv=\"refresh\" content=\"6;URL='http://YOURURL.com/'\"><br><br><br><div id='loading'><p><img src='loader.gif'> Please wait 6 seconds...</p></div></html>";

?>

Where the <meta http-equiv="refresh" content="6;URL='http://YOURURL.com/'"> tag is an HTML tag to tell the browser to change to the provided url after 6 seconds

To avoid adding the meta tag, you could also do this:

<?php
header('Refresh: 6;URL=http://www.YOURURL.com/');
echo "<html><br><br><br><div id='loading'><p><img src='loader.gif'> Please wait 6 seconds...</p></div></html>"
?>

But to be safe you should add both the header and the meta tag!

4
  • 2
    If you do that your header Location will not work since then headers were already sent.
    – tomsv
    Sep 6, 2012 at 11:22
  • You cannot send headers after the transfer of the content Sep 6, 2012 at 11:23
  • I know, I wrote about this in my answer, perhaps you didn't reload before my edit about 1 min ago
    – Pez Cuckow
    Sep 6, 2012 at 11:24
  • Thanks all of you! You were very helpful, my code works flawlessly now.
    – bsteo
    Sep 6, 2012 at 11:42
1

From PHP documentation:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

So you can't use the header() function after echo.

I suggest you to use javascript or the meta tag instead.

-1

I think you are looking for header('Refresh: 6; url=http://google.com/'); to do redirect

buffering and script sleep is not needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.