Forum Moderators: coopster

Message Too Old, No Replies

Can't set my cookie.

Yet again that "Cannot modify header information"

         

tomda

6:25 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I have the following error message.
"Cannot modify header information headers already sent by [...] include/header_php.php:253)"

I know very well what this message means but I can't find any blank space or any other outputs... By the way, header_php is my main include config file.

The funniest part is that:
A/ When I use setcookie at the last line of the include file (line 254), it works.
B/ And when I use setcookie just after calling the include (header.php), I get this message.

Rrrrr... So annoying!
Any help?

JAB Creations

7:29 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm far from being a PHP guru but I keep all my header related content explicitly limited to my header.php file and always include that file first if I include it on any page. If it's not header related don't include it in your header.php file.

If I have something that needs to set a cookie for example I have a second header file included called header-cookies.php that is included in the header.php file. Try something out like that. Not sure what your setup is and it might just be way above my head. :-D

- John

tomda

8:29 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks JAB for the tip...

I was too tired looking for that "ghost content" so I have decided to use the good old ob_start() and ob_end_flush().

Since I only setcookie in my login/logout page, it will do for the time being...

Thanks

jatar_k

1:54 pm on May 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



a common problem is a blank line before your opening php tag as that counts as output.

joelgreen

4:44 pm on May 18, 2007 (gmt 0)

10+ Year Member



Also some editors insert special characters (Byte Order Mark, [en.wikipedia.org...] ) which are not usually visible from editor. Seems to me Dreamweaver or Front Page inserts those.

BananaFish

5:32 am on May 22, 2007 (gmt 0)

10+ Year Member



You may also use page buffering, turn it on by calling ob_start(). If you're not doing any special handling, the output will automatically show when the page/script is done (you don't have to call any other buffering functions). You'll probably find it also increases the performance as well.

digitalracks

11:35 am on May 22, 2007 (gmt 0)

10+ Year Member



a common problem is a blank line before your opening php tag as that counts as output.

Very correct, it also happens if there is a space after the closing PHP tags?>

delboy1978uk

12:17 pm on May 22, 2007 (gmt 0)

10+ Year Member



absolutely anything that is sent to apache
(and like already mentioned, even a blank space) means that it wont work without either putting your php before the blank space, removing it, or using the output buffering.
Most of the time when i get this problem it is right there on line 1 of my code:

<?php etc etc

and not

<?php etc etc

what happens is apache gets the blank space, then php kicks in, but its too late, apache has started processing the page

using output buffering, all the php is processed first then everything is sent to apache, rather than as and when it is called.

:-)