Code Comments
Programming Forum and web based access to our favorite programming groups.Why if I use warning header("Location: file.php") i had this warning?
Warning: Cannot modify header information - headers already sent by
Post Follow-up to this messageShearer wrote:
> Why if I use warning header("Location: file.php") i had this warning?
> Warning: Cannot modify header information - headers already sent by
>
>
There is a space/tab (whitespace) or something else that has been outputted
before you use your header(), it's not allowed to output anything before a
header().
--- example whitespace ---
<?PHP
header("Location: file.php");
?>
--- eof ---
--- example HTML output ---
<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<?PHP
header("Location: file.php");
?>
--- eof ---
--- example echo to early ---
<?PHP
if(true) {
echo "Hello";
}
header("Location: file.php");
?>
--- eof ---
--- ok use of header ---
<?PHP
if(true) {
header("Location: file.php");
exit;
}
echo "Hello";
?>
--- eof ---
Only the last one will work.
//Aho
Post Follow-up to this message"J.O. Aho" <user@example.net> wrote in message news:4htondF18nnvU1@individua
l.net...
> --- example whitespace ---
>
> <?PHP
> header("Location: file.php");
> ?>
> --- eof ---
> Only the last one will work.
>
> //Aho
Unless of course output_buffering is not set to "off" in the php.ini.
-Lost
Post Follow-up to this messageShearer wrote:
> Why if I use warning header("Location: file.php") i had this warning?
> Warning: Cannot modify header information - headers already sent by
1) NOTHING must be output BEFORE a header() call.
2) I believe header('Location: URL') wants a complete absolute URL.
ie: http://www.example.com/file.php.
f.
Post Follow-up to this message"Cujo" <fra@despammed.com> wrote in message news:4hur18F19qr1U2@individual.n
et...
> 1) NOTHING must be output BEFORE a header() call.
>
> 2) I believe header('Location: URL') wants a complete absolute URL.
> ie: http://www.example.com/file.php.
Entirely untrue. If you utilize the output buffer directive, it eliminates
this problem.
Also, header (location) can take any valid URL handle as an argument. Or mo
re
appropriately/specifically:
"HTTP/1.1 requires an absolute URI as argument to » Location: including the
scheme,
hostname and absolute path, but some clients accept relative URIs. You can u
sually use
$_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolut
e URI from a
relative one yourself:"
And as far as I know *all* major browsers still support relative URIs. So,
it is kind of
like the W3C war on standards. It is *supposed* to be one way, yet <insert
a bunch of bad
ways> all still work just fine.
-Lost
Post Follow-up to this message-Lost wrote: > "Cujo" <fra@despammed.com> wrote in message news:4hur18F19qr1U2@individual .net... > > > Entirely untrue. If you utilize the output buffer directive, it eliminates this p roblem. It does not *eliminate* the problem, rather it works around it by delaying output to the client. The client still *MUST* receive the headers before anything else... I prefer to code cleanly in the first place, rather than have my php interpreter rearrange the output for me. > And as far as I know *all* major browsers still support relative URIs. So , it is kind of > like the W3C war on standards. It is *supposed* to be one way, yet <inser t a bunch of bad > ways> all still work just fine. I *STILL* prefer to code following standards, rather than "what seems to work". Especially when there is *NO REASON* to deviate from the standards. Build absolute urls on the fly in you need, no big deal. regards, f.
Post Follow-up to this message>> "Cujo" <fra@despammed.com> wrote in message news:4hur18F19qr1U2@individual.net... > It does not *eliminate* the problem, rather it works around it by delaying output to the > client. The client still *MUST* receive the headers before anything else...[/color ] It eliminates the problem of "nothing must be output before a header() call" . Also, I am not entirely sure how the "must receive the headers before..." go es. With an output buffer of xxxx (bytes) I could throw a wee bit of HTML and a Location header for example into the document and as long as it is sent at the *same* time as th e content, it is just fine. Do not forget, having the output buffer store some of this information is th e same thing as using ob_ functions. You are buffering output (and you can buffer header s!) and headers are always delivered before the buffer. Or is there something I am missing? > I prefer to code cleanly in the first place, rather than have my php inter preter > rearrange the output for me. I could not agree with you more. > I *STILL* prefer to code following standards, rather than "what seems to w ork". > Especially when there is *NO REASON* to deviate from the standards. Build absolute urls > on the fly in you need, no big deal. I still gotta' agree with you. I just wanted to state the facts about header(). Being that it does not *re quire* the standard. The standard requires absolute URIs and URL handles. Be well. -Lost
Post Follow-up to this messageOn Sun, 16 Jul 2006 14:51:00 +0200, Cujo <fra@despammed.com> wrote:
>Shearer wrote:
>
>
>1) NOTHING must be output BEFORE a header() call.
>
>2) I believe header('Location: URL') wants a complete absolute URL.
> ie: http://www.example.com/file.php.
The URL simply needs to be valid. You can do something like:
header("Location:index.html");. It will treat the link as localhost.
>f.
David
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.