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.
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).
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
Bastien Koert wrote:
The commands start and use an output buffer, a chunk of memory that stores all the output on the server until the ob_flush, the buffer flush, function is called. You need to be aware that in high load situations this may adversly affect performance since it loads the servers memory, also be aware that you may hit a memory limit for large pages. This is server / OS depandant so you may want to read up on server documentation
bastien
----------------------------------------
To: php-general@xxxxxxxxxxxxx; wouter@xxxxxxxxxxxxxxxxxx
Date: Sat, 18 Aug 2007 16:39:29 +0200
From: otto.wyss@xxxxxxxxxx
CC: kelvinpark86@xxxxxxxxx; php-general@xxxxxxxxxxxxx
Subject: Re: Cookies and sent headers
ob_start() at the beginning and ob_end_flush() at the end of the PHP
section seems to do the trick albeit I've still problems to understand
why. The description in the manual is rather sparse unfortunately. Is
there any more information about what's going on?
O. Wyss
Wouter van Vliet / Interpotential wrote:
You best option would be to go through all of your include'd or require'd
files and make sure there is no whitespace before and after you open your
php tags. Those are often the cause for such problems. The easy way would
indeed be to use output buffering. In that case, put the call to ob_start();
on the first line of the file you're calling. You will still have to make
sure to not have any whitespace before your >
To even bypass that, the output_buffering ini setting might be useful. Alter
it in your php.ini if you can, otherwise try your apache vhost configuration
or .htaccess. The syntax there is:
php_flag output_buffering On
Good luck!
On 18/08/07, Kelvin Park wrote:
Kelvin Park wrote:
Otto Wyss wrote:
If built a simple login page and store any information within
$_SESSION's. Yet I'd like to move these into cookies but I always get
an error about sent headers. Is there a way to circumvent this
problem without changing too much in the page?
The setting of the cookies happens just at the end of the page.
if (!$errortext and $Anmelden) {
if (!empty($Permanent)) {
$expires = time()+ 365 * 86400; // 365 days
setcookie ("l.Lastname", $_SESSION['l_Lastname'], $expires);
setcookie ("l.Firstname", $_SESSION['l_Firstname'], $expires);
setcookie ("l.Email1", $_SESSION['l_Email1'], $expires);
setcookie ("l.Email2", $_SESSION['l_Email2'], $expires);
}
echo "";
exit;
}
O. Wyss
ob_start() might help
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
_________________________________________________________________
Discover the new Windows Vista
http://search.msn.com/results.aspx?q=windows+vista&mkt=en-US&form=QBRE
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php