Re: setcookie command

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux