View Full Version : PHP wont set a cookie "Headers Already Sent.." ?

01-08-2007, 10:09 AM
Hey everyone..

I am trying to set a cookie so that a user does not have to enter their name into a text field every time they submit a comment...

I have a check box on the submission page and check for its value on the proccessing page like this :

if ($remember) { setcookie('rantname','$name', mktime()+86400, '/'); }

$remember is the variable that I grab from the post of the check box, I know it is working cus I tested that if statement with a text echo, but I am getting the following error (on that line of code) :

Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/nicholas/braddanick.com/head.php:7)

head.php is an included php page that contains my menu..

can anyone give me a hand?

01-08-2007, 10:38 AM
if you include your head.php file before setting the cookie, then it executes that before setting the setcookie() function, which is considered a header function. All headers get sent as soon as HTML, or any other client side script is run (and they're only sent once).

And based on your error, that's at line 7 in head.php.

I run header functions, mainly header(), all throughout a lot of my scripts, so I basically have one file called layout.php that contains the entire site layout, instead of separate head and foot files. I use a variable like $pagecontent (which has all the page content assigned to it from other files) in layout.php where all page content goes. Then just include layout.php at the very bottom of all my other PHP files. That way I don't get this problem because no HTML gets executed until last.

If it wouldn't be too much trouble, try that method.

01-08-2007, 01:19 PM
wow, thanks a lot man. I really didnt know about all that.

I would try your method, the thing is that my site is so jumbled up that I would have to re-design it.. it isnt a serious site, just something fun for friends, I just started a little forum there..

Check it out : http://www.braddanick.com

Thanks again!