Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    PHP wont set a cookie "Headers Already Sent.." ?

    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 :

    PHP Code:
    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?
    -DR.Wong

    Wheres the food at?

  • #2
    Regular Coder
    Join Date
    Nov 2006
    Posts
    247
    Thanks
    13
    Thanked 26 Times in 24 Posts
    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.
    Last edited by Majoracle; 01-08-2007 at 10:06 AM.

  • #3
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    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!
    -DR.Wong

    Wheres the food at?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •