Bluehost.com Web Hosting $6.95

warning header("Location: file.php")

This is a discussion on warning header("Location: file.php") within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Why if I use warning header("Location: file.php") i had this warning? Warning: Cannot modify header information - ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-16-2006
Shearer
 
Posts: n/a
Default warning header("Location: file.php")

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


Reply With Quote
  #2 (permalink)  
Old 07-16-2006
J.O. Aho
 
Posts: n/a
Default Re: warning header("Location: file.php")

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
Reply With Quote
  #3 (permalink)  
Old 07-16-2006
-Lost
 
Posts: n/a
Default Re: warning header("Location: file.php")

"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


Reply With Quote
  #4 (permalink)  
Old 07-16-2006
Cujo
 
Posts: n/a
Default Re: warning header("Location: file.php")

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.
Reply With Quote
  #5 (permalink)  
Old 07-16-2006
-Lost
 
Posts: n/a
Default Re: warning header("Location: file.php")

"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


Reply With Quote
  #6 (permalink)  
Old 07-16-2006
Cujo
 
Posts: n/a
Default Re: warning header("Location: file.php")

-Lost wrote:

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


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.
Reply With Quote
  #7 (permalink)  
Old 07-16-2006
-Lost
 
Posts: n/a
Default Re: warning header("Location: file.php")

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


> 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


Reply With Quote
  #8 (permalink)  
Old 07-16-2006
David
 
Posts: n/a
Default Re: warning header("Location: file.php")

On Sun, 16 Jul 2006 14:51:00 +0200, Cujo <fra@despammed.com> wrote:

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


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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:41 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0