Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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



Report this thread to moderator Post Follow-up to this message
Old Post
Shearer
07-16-06 02:57 AM


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

Report this thread to moderator Post Follow-up to this message
Old Post
J.O. Aho
07-16-06 02:57 AM


Re: warning header("Location: file.php")
"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



Report this thread to moderator Post Follow-up to this message
Old Post
-Lost
07-16-06 12:56 PM


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.

Report this thread to moderator Post Follow-up to this message
Old Post
Cujo
07-16-06 12:56 PM


Re: warning header("Location: file.php")
"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



Report this thread to moderator Post Follow-up to this message
Old Post
-Lost
07-16-06 12:56 PM


Re: warning header("Location: file.php")
-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.

Report this thread to moderator Post Follow-up to this message
Old Post
Cujo
07-16-06 11:56 PM


Re: warning header("Location: file.php")
>> "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



Report this thread to moderator Post Follow-up to this message
Old Post
-Lost
07-16-06 11:56 PM


Re: warning header("Location: file.php")
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

Report this thread to moderator Post Follow-up to this message
Old Post
David
07-16-06 11:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:29 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.