For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > July 2006 > warning header("Location: file.php")









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author warning header("Location: file.php")
Shearer

2006-07-15, 9:57 pm

Why if I use warning header("Location: file.php") i had this warning?
Warning: Cannot modify header information - headers already sent by


J.O. Aho

2006-07-15, 9:57 pm

Shearer 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
-Lost

2006-07-16, 7:56 am

"J.O. Aho" <user@example.net> wrote in message news:4htondF18nnvU1@individual.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


Cujo

2006-07-16, 7:56 am

Shearer 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.
-Lost

2006-07-16, 7:56 am

"Cujo" <fra@despammed.com> wrote in message news:4hur18F19qr1U2@individual.net...

> 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 more
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 usually use
$_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute 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


Cujo

2006-07-16, 6:56 pm

-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 problem.


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 <insert 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.
-Lost

2006-07-16, 6:56 pm

>> "Cujo" <fra@despammed.com> wrote in message news:4hur18F19qr1U2@individual.net...
[color=darkred]
> 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...


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..." goes. 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 the content, it
is just fine.

Do not forget, having the output buffer store some of this information is the same thing
as using ob_ functions. You are buffering output (and you can buffer headers!) 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 interpreter
> rearrange the output for me.


I could not agree with you more.

> 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.


I still gotta' agree with you.

I just wanted to state the facts about header(). Being that it does not *require* the
standard. The standard requires absolute URIs and URL handles.

Be well.

-Lost


David

2006-07-16, 6:56 pm

On 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com