You cannot put any output before an header, it just won't work and you will get an error. check your error report in php.ini.
You can also redirect in html like this:

<META HTTP-EQUIV="Refresh" CONTENT="5; URL=http://localhost/index.php">

If you want to use the header function, check the PHP manual.

later, Joeri

    you can display information before the header information, doing exactly what he is doing there

    using ob_start();

      tuf-web.co.uk doesn't know what he's talking about, as munjewerf said, you can't send output before sending HTTP headers, it's against the HTTP protocal entirely and just plain don't work.

      Use an HTML meta redirect, as muntjewerf demonstrated, instead.

        There's no point trying to send content before a header - that's just buggy. And it's pointless if the header is a redirect ("Here's some stuff - go to this page instead").

        It's equally pointless if content is sent after a header redirect because the client will never look at it - it's already processing the redirect. This is equally true whether the content is being sent after the header because of output buffering or not.

        If you want to show something and then have the client redirected, then META Refresh is the thing.

          Originally posted by superwormy
          tuf-web.co.uk doesn't know what he's talking about, as munjewerf said, you can't send output before sending HTTP headers, it's against the HTTP protocal entirely and just plain don't work.

          Use an HTML meta redirect, as muntjewerf demonstrated, instead.

          He is sort of correct.
          You can echo info before the headers are sent using ob_start().
          In this case it is totally useless, as has been pointed out.

          HalfaBee

            NO FRICK NO!

            HTTP HEADERS ARE ALWAYS SENT BEFORE THE DATA. THATS THE WAY HTTP WORKS!

            ob_start() simply tells PHP to NOT SEND DATA UNTIL THE HEADERS ARE SENT!

            So it waits until the script ends or you call ob_end_flush() and then SENDS THE HEADERS, AND THEN SENDS THE DATA!

            You might be placing the echo() or print() before the calls to header() but never-the-less, PHP isn't going to send any of your actual data UNTIL AFTER IT SENDS THE HEADERS!

            So to conclude this thread, you CANNOT, I repeat, CANNOT send data before HTTP HEADERS. You can tell PHP to not send the data until after all headers are sent ( thats what ob_start() is esentially doing, collecting data until all the headers are sent and then sending data ) but you can't send data then headers.

            If you look at an HTTP request, the HTTP request / response is actually just plain text placed before the data. The only reason you don't see it is because your browser hides it. If you sent data, then the headers, the browser wouldn't know what to display it as, and the HTTP response would probably get printed out somewhere in the middle of your HTML page.

              I have seen this feature on this board also. When you start a new thread, It shows some message ....then it takes you back on your posted message. How this board does this...

                Its an HTML meta-redirect tag.
                <meta http-equiv="Refresh" content="5; url=http://domain.com/page_to_direct_person_to.php">

                5 is the number of seconds to wait before redirecting them. It should be noted that Lynx ( and I imagine some other user-agents ) don't obey this tag automatically, you have to tell them to.

                  If you watch the URL, you'll notice that it changes; the intermediate page has a META refresh tag in it.

                    Thanks you all for the solution of my problem.

                    🙂

                      Originally posted by superwormy
                      NO FRICK NO!

                      HTTP HEADERS ARE ALWAYS SENT BEFORE THE DATA. THATS THE WAY HTTP WORKS!

                      ob_start() simply tells PHP to NOT SEND DATA UNTIL THE HEADERS ARE SENT!

                      So it waits until the script ends or you call ob_end_flush() and then SENDS THE HEADERS, AND THEN SENDS THE DATA!

                      You might be placing the echo() or print() before the calls to header() but never-the-less, PHP isn't going to send any of your actual data UNTIL AFTER IT SENDS THE HEADERS!



                      So to conclude this thread, you CANNOT, I repeat, CANNOT send data before HTTP HEADERS. You can tell PHP to not send the data until after all headers are sent ( thats what ob_start() is esentially doing, collecting data until all the headers are sent and then sending data ) but you can't send data then headers.

                      If you look at an HTTP request, the HTTP request / response is actually just plain text placed before the data. The only reason you don't see it is because your browser hides it. If you sent data, then the headers, the browser wouldn't know what to display it as, and the HTTP response would probably get printed out somewhere in the middle of your HTML page.

                      If that rant was in response to my post you should try reading.

                      HalfaBee

                        this, however useless will NOT produce an error, and WILL redirect

                        <?
                        ob_start();
                        echo "blah, blah, blah....";
                        header("Location: pagetoredirect.php");
                        ?>
                        

                        you won't see the echo, but it will redirect, ob_start() suppresses the header errors, i suggest against it, but it DOES work, and YES is somewhat buggy

                          Originally posted by HalfaBee
                          If that rant was in response to my post you should try reading.

                          HalfaBee

                          Well, what you said was

                          You can echo info before the headers are sent using ob_start().

                          True, you didn't explicitly say that any output was produced (and that the output is delayed until later); but that is the usual interpretation of the verb "echo".

                            I didn't mean to use echo as a verb, but as the language construct echo.

                            Using ob_start() and setting the headers later is not "Buggy", sometimes it is useful to construct part of the page first and then send headers ( not always the redirect header ) .

                            HalfaBee

                              Don't know if this help anyone or not.
                              Since superwormy already closed this thread it probably doesn't matter now.

                              This works

                              //page1.php
                              <?
                              header ("Refresh: 5; URL=page2.php");
                              echo "This is an echo. You should see me for 5 seconds.";
                              <?
                              

                              D.

                                Write a Reply...