You are here: Re: [PHP] Location .... « PHP « IT news, forums, messages
Re: [PHP] Location ....

Posted by "Richard Lynch" on 12/30/05 22:04

On Wed, December 28, 2005 8:15 am, Christian Ista wrote:
> PHP? I tried this code : header("Location: mypage.php");
>
> But in some case, I have error message "headers already sent". Then I

You know the old saying, "You can't step in the same stream twice"?

Think about it.

Really think about it.

Now...

The data being sent to the browser from Apache/PHP is a stream.

Its raw output looks something like this:

---------------------------------------------------------------
HTTP/1.1 200 OK
Date: Fri, 30 Dec 2005 19:52:44 GMT
Server: Apache/1.3.34 (Unix) DAV/1.0.3 PHP/4.4.1 mod_ssl/2.8.25
OpenSSL/0.9.7d-p1
X-Powered-By: PHP/4.4.1
Connection: close
Content-Type: text/html

<HTML>
<HEAD>
<TITLE>Lynch Interplanetary Enterprises</TITLE>
</HEAD>
..
..
..
</HTML>
---------------------------------------------------------------
Now, pay VERY close attention to that blank line right after the
Content-type: text/html
line.

That BLANK line marks the end of the "headers"

Everything above the blank line is in the "headers"
Everything below is the "body" of the HTTP, which is what the browser
shows you with "View Source"

Now, imagine all that output, headers and blank line and body,
streaming out from your server, like water from a garden hose.

There's a lot you can do to it before the water gets shot out the end
of the nozzle.

You could add a water heater and treat your lawn to a warm shower
instead of cold.

You could add a filtration system and give your lawn distilled water.

You could add a plant food system to give it yummy chemicals.

Buuuuuuuuuuuuuuuuuuuuuut

Once that water leaves the host, you don't have a lot of control over it.

It's gone.

Beyond re-calling.

In the same way, once PHP sends the BLANK LINE to denote the end of
headers, you can't send more headers.

This works:

---------------------------------------------------------------
HTTP/1.1 200 OK
Date: Fri, 30 Dec 2005 19:52:44 GMT
Server: Apache/1.3.34 (Unix) DAV/1.0.3 PHP/4.4.1 mod_ssl/2.8.25
OpenSSL/0.9.7d-p1
X-Powered-By: PHP/4.4.1
Connection: close
Content-Type: text/html
Location: mypage.php

<HTML>
<HEAD>
<TITLE>Lynch Interplanetary Enterprises</TITLE>
</HEAD>
..
..
..
</HTML>
---------------------------------------------------------------

This can NOT work:

---------------------------------------------------------------
HTTP/1.1 200 OK
Date: Fri, 30 Dec 2005 19:52:44 GMT
Server: Apache/1.3.34 (Unix) DAV/1.0.3 PHP/4.4.1 mod_ssl/2.8.25
OpenSSL/0.9.7d-p1
X-Powered-By: PHP/4.4.1
Connection: close
Content-Type: text/html

<HTML>
Location: mypage.php
<HEAD>
<TITLE>Lynch Interplanetary Enterprises</TITLE>
</HEAD>
..
..
..
</HTML>
---------------------------------------------------------------

Look carefully for the position of the line
Location: mypage.php

See the difference?

If you try to call the header() function AFTER some content has been
sent, then you get the error message.

Move the header() line earlier in your PHP code, and it will work fine.

PS
Blank lines and spaces COUNT as "output".

So this:
<?php $x = 1; ?> <?php header("Location: mypage.php");?>
will NOT work because that space "counts"

This will work:
<?php $x = 1; ?><?php header("Location: mypage.php");?>

Similarly, watch out for BLANK LINES after the last ?> in your files.

They will trip you up for header()

--
Like Music?
http://l-i-e.com/artists.htm

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация