Lots of people run into problems with the header function and I don't really understand why. It requires that you have a basic knowledge or the interaction between client and server (not just what your script is doing) and what other information is actually being sent apart from your scripts output.
In this sense PHP is a VERY forgiving langauge. It automagically sends out any headers it feels appropriate, and cleans everything up for you so you only have to worry about your code (try taking a look at Perl, a lot less forgiving, and you MUST send out a Content-type header EVERY time).
Anyhow - to get to the point behind this posting....
a header('Location ..... blah blah blah should normally be followed by an "exit();". The reason? Most people want page processing to stop after the Location header has been sent, but PHP actually will continue process PHP script until it finishes. There is no reason why a Location header should stop page processnig, the browser will still accept output even with this header (and the header may not be actually sent until the script is finished and the stream is closed.
Some people will also try dumping a header AFTER some content has already been sent to the client. The headers should be the first thing you send, whether you buffer (and then flush) previous content, or put off printing anything out until you have decided no more headers need to be sent. Most peoples problems occur here because of a leading space or enter character at the beginning of their script (before the <? operator which gets dumped straight to the browser meaning any required headers will be sent).
Javascript redirection is all well and good - but what if a client has disabled Javascript - or for some reason the Javascript fails?
Because of all these issues, and having to determine whether output has been sent I have developed a function which will try and stop most of these issues.
Now - I am against giving complete solutions out but the gist of it goes like this.... (this is off the top of my head so please forgive me any typos etc - just look on it as leaving you something to do!)
function do_redirect($strPage)
{
if (headers_sent())
{
// DO a javascript redirection
// Apologies for JS errors
print('<html><head><script language="JavaScript">document.onload=function () {document.top.location.href="' . $strPage . '"};</script></head><body> </body></html>');
}
else
{
// Headers have not been sent to redirect using a header
header('Location: ' . $strPage); // No \n's or \r's needed since PHP will look after that for us - told you it was forgiving!
}
// Now we stop any more output - since we are redirecting
exit();
}
You can see that the JS redirection prints out an entire simple page. This is to maintain maxuimum compatability across all platforms/browsers etc.
Anyhow - enought dribble. Lemme know what ya think