Cannot modify header information - headers already sent by

Discussion in 'PHP' started by sin0cide, Dec 8, 2007.

  1. #1
    I have checked for whitespace and anything other normal problems but don't see anything if there something else that might be causeing this?

    <head>
    <?php
      if (isset($_POST["basic"])) 
    	setcookie("basic","1",time()+3600);
      if (isset($_POST[full]))
    	setcookie("full","1",time()+3600);
    ?>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    <?php 
      if (isset($_POST["basic"])) 
         echo "Basic Form from Post <br />";  
      elseif (isset($_COOKIE["basic"]))
         echo "Basic Form from COOKIE <br />";
      else
      	echo "No Basic Form <br />";
      if (isset($_POST["full"]))
         echo "Full Form from Post <br />";  
      elseif (isset($_COOKIE["full"]))
         echo "Full Form from COOKIE <br />";
      else
      	 echo "No Full Form <br />";
    ?>
    </body>
    Code (markup):
     
    sin0cide, Dec 8, 2007 IP
  2. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Right there, the very first line reads :"<head>"
    You must not have any html code preceding your php script. Include your php cookie code in the first line of that file.
     
    hogan_h, Dec 8, 2007 IP
  3. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That's right, no output before a header is sent (cookies and session variables are part of the header info), you have 2 solutions :

    1 -
    
    <?php
      if (isset($_POST["basic"])) 
    	setcookie("basic","1",time()+3600);
      if (isset($_POST[full]))
    	setcookie("full","1",time()+3600);
    ?>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Unt
    PHP:
    2 - Use the OB function (OB : output buffer I think)
    <?php ob_start; ?>
    
    //anything here, PHP will not throw errors related to header info
    
    <?php ob_end_flush(); ?>
    PHP:
     
    selling vcc, Dec 9, 2007 IP