Re: setcookie command
| From: | Richard Lynch | Date: | Mon, 24 Jan 2005 19:01:00 +0000 |
| Subject: | Re: setcookie command | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-207143@lists.php.net to get a copy of this message | ||
Reza Milan wrote:
> Dear sir,
>
> When I use setcookie command I recieve the following message error:
>
> Warning: Cannot modify header information - headers already sent by
> (output started at C:\Inetpub\wwwroot\test.php:9) in
> C:\Inetpub\wwwroot\test.php on line 15
>
> WinXp - IE 6
> On Localhost and Server
When a web page is sent from the web server to the browser, there are
*TWO* distinct sections sent out.
First, is the 'headers', which are called 'headers' because they come at
the head (beginning) of the data.
Then comes a blank line, to signify the END of headers.
Finally, the 'body' comes out, which is what you see when you do "View
Source" -- It's the actual HTML.
Headers include meta-information such as the type of Content (text/html or
image/jpeg or image/gif or ...)
In your case, test.php has something on line 9 that is sending out some
HTML (or blank lines or ...)
Once that happens, PHP has to dump out all the headers collected so far,
and then a blank line, and then start your content.
If you try to set a cookie *AFTER* that, when the headers are already
gone, and the blank line has gone out to signify the end of headers, IT'S
TOO LATE.
You have to re-arrange your code to get the Cookies and any other headers
to happen *FIRST* and then your HTML after that.
You can cheat and turn on "output buffering," but that comes with its own
performance penalties and issues.
You'll probably have a cleaner, more clear, more easily maintained body of
PHP code if you arrange your headers to come out first anyway.
--
Like Music?
http://l-i-e.com/artists.htm