Jump to content
Larry Ullman's Book Forums

Header File Already Sent

Recommended Posts

I'm working on passing cookies from my signin.php to my home page - I am using the exact logic as in the login.php script except for the redirection is named differently. I have 'echo'ed the header_sent function to see where this is happening and it happens before my  "if...submitted" statement. There is no html before that or in the php, and all the error messages are saved to print later...the form itself is html but I've written it exactly like the book - there still seems to be a header file from the first display of the form prior to entering any information in the fields...any way to close the header file after displaying the form the first time?


in the book it says something about "no empty lines outside of php tags" huh?

Link to comment
Share on other sites

Hello and welcome to the forums.

Could you please provide what you feel is the relevant code?

Thank you.

thank you so much for looking at this...below is the code.


question: there is no html on this page except for the form...where is the header coming from? I even took out the include for my headers page.


question: can headers come from previous pages?






  if (headers_sent()) {

     echo "<p>headers sent before submit</p>";}  //diagnostic to determing where the headers were coming in


  if (isset($_POST['submitted']))   //submitted test  AA


     {   if (headers_sent()){ echo "<p> header sent inside submitted</p>";} //diagnostic again - and I am always seeing this line


         require_once ('./mysqlconnection.php');

         $errors = array(); //initialize error array.



            //check for project email address




         if (empty($_POST['userid']))


            $errors[] = 'Please enter your email address';


         else {

              $em = escape_data ($_POST['userid']);                         





         //check for password


       if (empty($_POST['password']))


            $errors[] = 'Please enter a password';



        else {

              $pw = escape_data($_POST['password']);







       if (empty($errors))     //both fields are filled -BB





           // validate their email and password //





               //make the query



              $query = "SELECT userid, password, firstname, lastname, points FROM account  WHERE userid = '$em' AND password='$pw'";

              $result = @mysql_query($query); //run the query

              $row = mysql_fetch_array ($result, MYSQL_NUM);  //return a record




               if ($row) {       //found a match





                           if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\')) {

                                $url = substr($url,0,-1); //chop off slash                        


                                $url .= './loggedin.php';                                                

                           if (!headers_sent())


                                    setcookie ('userid',$row[0]);

                                    setcookie ('firstname',$row[2]);

                                    header("Location: $url");

                                    exit(); //quit script





          } //end of empty errors check  -BBclosed




$pagetitle = "Share a lead";

include ('./header.php');    // I even removed this but it still sent headers //


 // I took out all formating below to see if that would help...still sent headers but it seems like it has to be coming from the form //


<form action="signin.php" method="post">

                 <p><b>Email:<input type="text" name="userid" size="40" maxlength="40" /> </p>

                 <p><b>Password:<input type="text" name="password" size="10" maxlength="10"/> </p>



                <p><input type="submit" name="submit"     value="SignIn" /></p>

                   <input type="hidden" name="submitted"  value="TRUE" />




Link to comment
Share on other sites

I have a headers file that displays the menu. then click on signin.php to get to the page I sent you above.


I get the "headers already sent" everywheare on the page...question: if I am looking at the webpage displayed from the code above - are headers "active" at that time? Is there a header that has already been sent from the previous page? If so, how do you every get around that?


When I enter a valid email and password and press submit - headers have already been sent, I just don't know where from. my diagnostic check right after "If Submitted" already there. the only html on the page is for the form.


Any ideas?

Link to comment
Share on other sites

I'm sorry, to be more clear, if the code you've posted is included by another page, how about posting that other page's code? Also, instead of using your "headers already sent" bit, you could remove all those and if there is an error, the error would specifically indicate where the headers are already being sent (such and such file on this line). 


Headers, by the way, can be plain text, HTML, spaces, or blank lines. Anything outside of a PHP tag or anything that you've printed in PHP sends data to the browser. In the code you've provided above, I would imagine that output would be begun to the browser on this line:

include ('./header.php');

Again, though, if you take out those headers_sent() uses, you'll get an error message if there's a problem and the error will indicate exactly what's causing the problem.

Link to comment
Share on other sites


  • Create New...