php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20923 Buffering Prevents setcookie()
Submitted: 2002-12-10 11:09 UTC Modified: 2002-12-11 01:56 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: viszneki at telerama dot com Assigned:
Status: Closed Package: HTTP related
PHP Version: 4.2.3 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
33 - 21 = ?
Subscribe to this entry?

 
 [2002-12-10 11:09 UTC] viszneki at telerama dot com
I just went from PHP 4.1.2 to 4.2.3, and after some investigation, found that setcookie would absolutely not set cookies. So I wrote a test script to flip a cookie value each time you reloaded the script. But it worked just fine. I investigated some more. Totally confused, I wrote a script to flip a cookie again, but this time it didn't work.

It turned out that if I setcookie() after sending non-header data, that the cookie never showed up. I remembered that previously I would get a header error, and also remembered that in the PHP.INI that came with the new php, they had enabled output buffering. Changing the buffering settings seems to makes no difference.

My PHP Build was just one downloaded from the php.net downloads page, nothing special about it.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-10 11:19 UTC] viszneki at telerama dot com
I just wrote another script just to confirm the problem, and, well instead I did the opposite. This script seems to have upset my previous thoughts, so I'll keep trying to figure out what the problem is, it may be my own fault.

<?php
setcookie( "one", "uno", time()+10000 );
setcookie( "two", "dos", time()+10000 );
echo $_COOKIE['one'], '=1<br>', $_COOKIE['two'], '=2<br>', 

setcookie( "three", "tres", time()+10000 );
setcookie( "four", "quatro", time()+10000 );
echo $_COOKIE['three'], '=3<br>', $_COOKIE['four'], '=4<br>';
?>
 [2002-12-10 11:21 UTC] viszneki at telerama dot com
That previous script I wrote behaves strangely.

I erased my cookies, then loaded the page. As expected, no cookies were sent from my web browser, and the script simply said "=1 =2 =3 =4" with the BR tags between them.

I hit reload, and only 1 and 2 showed the appropriate cookies, Uno and Dos. I hit reload again, and now Tres and Quatro show up.

I did this process 3 times to make sure it was consistent, and it happens the same way every time.
 [2002-12-10 17:13 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php4-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php4-win32-latest.zip


 [2002-12-10 20:27 UTC] viszneki at telerama dot com
Using the CVS version Sniper asked me to use, NONE of the cookies were set using the before-mentioned script.
 [2002-12-10 21:36 UTC] viszneki at telerama dot com
I beleive the problem is in the PHP.INI file, I retardedly copied over the suggested ini file that came with php 4.2.3 as soon as i installed it, and no longer have my old ini file.

I just downgraded to php 4.1.2 and I'm having similar problems! Though the before-mentioned php script actually only sends the client one cookie per execution in my current setup. Additionally, my other scripts do not work under the current ini file even while using php 4.1.2.

I don't know if this information will actually help, but I suspect it should narrow it down at least a little bit.
 [2002-12-11 01:06 UTC] sniper@php.net
RTFM:

"setcookie() defines a cookie to be sent along with the rest 
of the HTTP headers. Like other headers, cookies must be 
sent before any output from your script (this is a protocol 
restriction)."


 [2002-12-11 01:25 UTC] viszneki at telerama dot com
You have gotta be kidding me sniper, haven't you been paying attention? Output buffering is enabled. And if it weren't, Tres and Quatro would NEVER be set, and I'd get an HTTP HEADER error printed out on the page.
 [2002-12-11 01:56 UTC] sniper@php.net
Yes, this was some bug which now seems to be fixed in 4.3.0-dev (as I can not reproduce this)

I stumbled into one other bug here, #20887, that's why I
got confused, sorry about that.




 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 15:01:28 2024 UTC