Re: Cookies and sent headers

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

 



M. Sokolewicz wrote:
On a sidenote, 99% of the world never calls ob_flush (or any such function) since PHP flushes the buffer automatically at the end of its execution.

I'll keep the ob_end_flush just for showing what's going on, but thanks for the hint.

The reason why setting cookies for you doesn't work is because of the way a HTTP response is structured. It consists of 2 parts: header and body separated by 2 new lines (\n\n). It is _required_ that _all_ headers come _before_ the body. Cookies are actually headers (a set-cookie: [...] header) aswell as any headers set via php's header() function. Any output made by the script (be it a single whitespace, a bunch of text, etc.) will automatically flush the headers, followed by the separator (\n\n) followed by the specified output. After that has been sent, everything outputted will be dumped into the body of the response (simply because you can't "go back" to the headers which were already sent earlier), so you can't set cookies (which are headers themselves).

Thanks, now it's understandable.

So, why does output buffering help here? Simply put, instead of dumping headers and any other output directly to the client, PHP buffers it all into memory. As such, since it hasn't been sent yet, PHP can still alter/add headers even though it also has body-output. Once it receives the command, PHP will send all headers, the separator and the output to the client. This is done when PHP encounters an ob_flush or ob_end_flush call, _and_ when the script has finished execution automatically.

The documentation seems pretty clear to me, however, if you feel it should be clarified better, feel free to send a patch to the php-doc@xxxxxxxxxxxxx list.

- Tul

O. Wyss

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